1002 大数相加

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

难点是,数据类型最长有32位(4字节或者2字),数值范围是-2147483648~2147483648或者0~4294967295,但题目中指出输入数据位数长度可以达到1000位,10^999>>4294967295,故不能用常规方法

具体解决方法是,将数字利用字符串的形式表示,每个字符都是数字,1000个连续字符也没问题,再将两个不同字符串相加得到最终结果。

有一次提交时,出现了“Presentation
Error”的错误,缘由是输出结果的格式不符合要求,比方少个空格什么的。


 1 #include <iostream>
2 #include <string>
3 using namespace std;
4 int main()
5 {
6 int n;
7 while(cin>>n)//n为case数
8 {
9 for(int i=1;i<=n;i++)
10 {
11 string a,b,c;//3个字符串
12 cin>>a>>b;
13 int la=a.length()-1,lb=b.length()-1,jw=0,ta,tb,tt,f=0;
14 char tc;
15 while(la>=0||lb>=0)
16 {
17
18 if(la<0) ta=0;
19 else ta=a[la]-‘0‘;
20 if(lb<0) tb=0;
21 else tb=b[lb]-‘0‘;
22 tt=jw+ta+tb;//tt为a和b两位相加结果
23 jw=tt/10;//jw为进位
24 tc=tt%10+‘0‘;//tc为赋值给字符串c之前的一个中转
25 if(tc!=‘0‘) f=1;//f为进位标志
26 c+=tc;
27 la--;lb--;
28 }
29 if(jw>0)
30 {
31 f=1;
32 tc=jw+‘0‘;
33 c+=tc;
34 }
35
36 if(i!=1) cout<<endl;
37 cout<<"Case "<<i<<":"<<endl;
38 cout<<a<<" + "<<b<<" = ";
39 if(f==1)
40 {
41 for(int j=c.length()-1;j>=0;j--)
42 cout<<c[j];
43 cout<<endl;
44 }
45 else cout<<0<<endl;
46 }
47 }
48 return 0;
49 }

时间: 2024-10-18 16:36:43

1002 大数相加的相关文章

杭电OJ 1002 大数相加

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 fol

2016&quot;百度星&quot;资格赛1002 大数相加

不多说 如题 为了保存代码 1 #include<iostream> 2 #include<climits> 3 #include <cstdlib> 4 #include <string.h> 5 6 using namespace std; 7 8 static char chart[201][45]; 9 static int p = 2; 10 11 void setChart(int N); 12 13 int main() 14 { 15 int

ACM 大数相加

大数问题 基本都可以归结到大数相加上来 做大数问题  要返璞归真 回到小学里做加法 把数字读入到字符串数组中  每个位数一 一相加  主要考虑进位问题 如果整数的话 左边用零补齐 小数的话要左右分开补齐零  小数的零要补右边 HDOJ题目在1002 1753 下面给代码  整数相加 #include <stdio.h> #include <string.h> #define SIZE 1000 void convert(char a[],char newa[]) { memset(

java-两个大数相加

题目要求:用字符串模拟两个大数相加. 一.使用BigInteger类.BigDecimal类 public static void main(String[] args) { String a="8888899999999888";  String b="88888888888888";  String str=new BigInteger(a).add(new BigInteger(b)).toString();  System.out.println(str);

高精度问题之大数相加(原来就是用字符串相加,模拟手算这么简单!)

解题心的: 就是基本的一对一模拟手算..借助c++的string 不用逆序运算了.很方便的补0.  最后处理下前导0的问题. #include <iostream> #include <string> using namespace std; // 实现大数相加 结果存放在num中 void bigIntergerAdd(string &num, string add) { int goBit = 0; // 存放进位 // 先交换下顺序 加数的位数要比较少 if (num

HDU 1047 Integer Inquiry 大数相加 string解法

本题就是大数相加,题目都不用看了. 不过注意的就是HDU的肯爹输出,好几次presentation error了. 还有个特殊情况,就是会有空数据的输入case. #include <stdio.h> #include <vector> #include <string.h> #include <algorithm> #include <iostream> #include <string> #include <limits.h

模板,大数相加

char a[Max],b[Max],c[Max],sum[Max]; void jia(char str1[],char str2[]) { int i,j,k,z; k=0;z=0; for(i=strlen(str1)-1,j=strlen(str2)-1;i>=0||j>=0;i--,j--) //核心,加法以及进位 { if(i>=0) z+=str1[i]-'0'; if(j>=0) z+=str2[j]-'0'; c[k++]=z%10+'0'; z=z/10; }

两个大数相加 ----Javascrit 实现

(function(){ var addLarge = function(n1,n2){ var over = 0; var ret = ""; var len = Math.min(n1.length,n2.length); var sln1 = n1.substr(n1.length - len,n1.length ); var sln2 = n2.substr(n2.length - len,n2.length ); for(var i = len;i > 0; i--)

hdu acm-1047 Integer Inquiry(大数相加)

Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11678    Accepted Submission(s): 2936 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He ex