(string高精度)A + B Problem II hdu1002
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 399645 Accepted Submission(s): 77352
Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line is the an equation "A + B = Sum", Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
用JAVA
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n;
BigInteger a,b;
n=in.nextInt();
int i=0;
while(n>0) {
n--;
a=in.nextBigInteger();
b=in.nextBigInteger();
i++;
System.out.println("Case" + " " + i + ":");
if(n>0) {
System.out.println(a + " + " + b + " = "+a.add(b));
System.out.println();
}
else {
System.out.println(a + " + " + b + " = "+a.add(b));
}
}
}
}
C++:
要审清题意,注意输出格式!
#include<string>
#include <cstdio>
#include<iostream>
using namespace std;
string add(string a,string b)
{
int len1=a.length();
int len2=b.length();
if(len1>len2)
{
for(int i=;i<=len1-len2;i++)
b=""+b;
}
else
{
for(int i=;i<=len2-len1;i++)
a=""+a;
}
int len=a.length();
int cf=,t;
string str;
for(int i=len-;i>=;i--)
{
t=a[i]-''+b[i]-''+cf;
cf=t/;
t%=;
str=char(t+'')+str;
}
if(cf!=)
str=char(cf+'')+str;
return str;
}
int main()
{
int t;
cin>>t;
int k=;
while(t--)
{
k++;
string a,b;
cin>>a>>b;
string str;
str=add(a,b);
cout<<"Case "<<k<<":"<<endl;
cout<<a<<" + "<<b<<" = "<<str<<endl;
if(t)
cout<<endl;
}
return ;
}
(string高精度)A + B Problem II hdu1002的更多相关文章
- hdu1002 A + B Problem II(高精度加法) 2016-05-19 12:00 106人阅读 评论(0) 收藏
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdoj 1002 A + B Problem II 高精度 java
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU1002 -A + B Problem II(大数a+b)
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- HDU 1002 A + B Problem II(高精度加法(C++/Java))
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- hdu1002 A + B Problem II(大数题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java/ ...
- HDU1002 A + B Problem II 大数问题
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1002 A + B Problem II Time Limit: 2000/1000 MS (Java ...
- A + B Problem II
之前总是在查阅别人的文档,看着其他人的博客,自己心里总有一份冲动,想记录一下自己学习的经历.学习算法有一段时间了,于是想从算法开始自己的博客生涯O(∩_∩)O~~ 今天在网上看了一道大数相加(高精度) ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- HDU 1002 A + B Problem II
A + B Problem II Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16104 Accepted ...
随机推荐
- Python-列表-9
列表: Why: 我们现在已经学过的数据类型有:数字,布尔值,字符串,大家都知道数字主要用于计算,bool值主要是条件判断,只有字符串可以用于数据的存储,这些数据类型够用么?对于一门语言来说,肯定是不 ...
- ACM-ICPC 2018 沈阳赛区网络预赛 G. Spare Tire
这题很好啊,好在我没做出来...大概分析了一下,题目大概意思就是求 问所有满足1<=i<=n且i与m互素的ai之和 最开始我们队的做法是类似线性筛的方法去筛所有数,把数筛出来后剩下数即可, ...
- 705 B. Spider Man
传送门 [http://codeforces.com/contest/705/problem/B] 题意 这题意看原文的真tm难懂Woc,但结合样例就知道大概意思了 两个轮流分环,可以这么理解两个人轮 ...
- SE Springer小组之《Spring音乐播放器》需求分析说明书二
2.1 目标 Spring音乐播放器软件为课程<软件工程>所开发的课程作业,主要意图是为访问计算机中的mp3格式的音频文件,并使其能够完成访问,读取,添加,保存,播放,切换音频文件等功能. ...
- “数学口袋精灵”App的第三个Sprint计划(总结与团队感悟)----开发日记
第三阶段Sprint完成情况: 我们的"数学口袋精灵"App已经完成了,该app能随机产生多种形式的算式,比如带括号的,分数四则运算,混合运算,阶乘等,通过游戏形式让用户乐在其中. ...
- There are no enabled repos.
今天要记录一下自己懵逼的一天,原来自己是Ubuntu系统,还以为是centos,导致命令错了 There are no enabled repos. Run "yum repolist al ...
- Prism6下的MEF:基于微软企业库的Cache
通常,应用程序可以将那些频繁访问的数据,以及那些需要大量处理时间来创建的数据存储在内存中,从而提高性能.基于微软的企业库,我们的快速创建一个缓存的实现. 新建PrismSample.Infrastru ...
- 『编程题全队』Beta 阶段冲刺博客三
1.提供当天站立式会议照片一张 2.每个人的工作 (有work item 的ID) (1) 昨天已完成的工作 孙志威: 1.添加登录框的功能 2.修改登录框的UI 孙慧君: 1.提醒显示UI设计: 2 ...
- 第八章Jdk代理 cglib代理
什么是代理模式 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象访问目标对象.这样做的好处是:可以在目标对象实现的基础上,增强额外的功能操作,即扩展目标对象的功能. 这 ...
- Tomcat启动错误一例org.apache.catalina.core.StandardContext resources Start Error starting static Resources
org.apache.catalina.core.StandardContext resources Start Error starting static Resources 引发原因:Eclips ...