A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 188211 Accepted Submission(s): 35956
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
Author
Ignatius.L
题意:
给出 T (1 ~ 20)个case,后给出两个数a,b,每个数的长度最多1000位,求两个数加和按要求格式输出。
思路:
输入的顺序是从高位向地位输入,故计算的时候从 n -> 0 计算,并且考虑进位情况。考虑到数的输入输出情况,故开 char 型数组比较方便,计算的时候相应减去字符 0 即可。
两个数的长度可能不一致,故先对其,前补0再计算,输出的时候不输出前导0,每个case之间要空一行,最后一个case不需要空行,大概注意这些就可以AC了。
AC:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int main() { int n,time = 0; scanf("%d",&n); while(n--) { int CIN = 0,len_a,len_b,len_sum; char a[1005],b[1005],sum[1005],temp[1005],temp_a[1005],temp_b[1005]; time++; scanf("%s%s",a,b); len_a = strlen(a); len_b = strlen(b); len_sum = max(len_a,len_b); strcpy(temp_a,a); strcpy(temp_b,b); if(len_a > len_b) { int idex_b = 0,i; for(i = 0;i < len_a - len_b;i++) temp[i] = '0'; for(i;i < len_a;i++,idex_b++) temp[i] = b[idex_b]; for(i = 0;i < len_a;i++) b[i] = temp[i]; b[i] = '\0'; } if(len_b > len_a) { int idex_a = 0,i; for(i = 0;i < len_b - len_a;i++) temp[i] = '0'; for(i;i < len_b;i++,idex_a++) temp[i] = a[idex_a]; for(int i = 0;i < len_b;i++) a[i] = temp[i]; a[i] = '\0'; } for(int i = len_sum - 1;i >= 0;i--) { int ans = (a[i] + b[i] - '0' - '0' + CIN); sum[i] = ans % 10 + '0'; CIN = ans / 10; } sum[len_sum] = '\0'; printf("Case %d:\n",time); printf("%s + %s = ",temp_a,temp_b); if(CIN) printf("%d",CIN); puts(sum); if(n) printf("\n"); } return 0; }
相关推荐
Description Calculate a+b Input Two integer a,b (0<=a,b) Output Output a+b Sample Input 1 2 Sample Output 3
可以计算任意位数的加法,C++实现,精度可达100位
FZYZOJA+B Problem.cpp
下载超过3次或对应题解文章点赞超过1个便开放免费下载!
洛谷题解:P1001-A+B Problem
输入A和B,求出A+B的值
#include using namespace std; int main() { int a,b; cin>>a>>b; cout<<a+b; }
在编程的时候供大家参考,希望各位参加 acm的同学好好努力,能够取得好成绩
NULL 博文链接:https://weitch.iteye.com/blog/1006774
Problem Description 话说,经过了漫长的一个多月,小明已经成长了许多,所以他改了一个名字叫“大明”。 这时他已经不是那个只会做100以内加法的那个“小明”了,现在他甚至会任意长度的正小数的加法。 现在,给你...
具体题目在我的博文当中
Uva 100 ,问题是The 3n+1 probelm ,可以ac的代码
北大POJ1207-The 3n + 1 problem 解题报告+AC代码
UVA 100题答案
Data+Abstraction+and+Problem+Solving+with+C+++3+Ed
MCM 2012年 problem A problem B problem C 枪手论文
《英语学习工具书总汇》作者:多人,多本英语工具书的集合!
任务一: 1 +B for Input-Output Practice(II);2 +B for Input-Output Practice (III);3 Sum Problem(II);4 +B for Input-Output Practice(IX);5 + B Problem Too; 任务 二:1 猴子吃桃;2 百鸡问题;3 求多个...
关于qx+1问题高度的探讨,蒋愉,吴栋梁,Collatz问题,也称为3x+1问题,从上个世纪开始就一直被众多学者所关注和研究。到目前为止,已经有了很多不错的结果。本文将主要探讨q
有关3x+1问题的一些新结果,蒋愉,吴栋梁,本文主要研究了公开的数学难题--3x+1问题。利用专业数学软件Mathematica 5.0,我们给出了一个较R.E.Crandall之前给出的更为完整和丰富的表格