1C - A + B Problem II
2024-10-14 15:58:20
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 // too young and didn't notice that
// "you should not process them by using 32-bit integer"
// and "Output a blank line between two test cases".
#include<stdio.h>
int main()
{
int a, b, t, i;
scanf("%d", &t);
for(i=;i<=t;i++)
{
scanf("%d %d", &a, &b);
printf("Case %d:\n", i);
printf("%d + %d = %d\n", a, b, a+b);
printf("\n");
}
return ;
}
Wrong Answer
// long int is also 32-bit integer.
代码省略
// 不用判断哪个数较长,只要记录答案的长度. 三个数组都要初始化为0!!!
#include<stdio.h>
#include<string.h> int reverse_add(int *a, int *b, int *c, int al, int bl)
{
int i, sum=, k, max;
if(al<bl) max=bl;
else max=al;
k=;
for(i=; i<max; i++)
{
*(c+i)=(*(a+i)+*(b+i)+k)%;
k=(*(a+i)+*(b+i)+k)/;
}
if(k!=)
{
*(c+i)=;
return max+;
}
else return max;
} int main()
{
char s1[], s2[];
int t, k, i, length_a, length_b, rmax;
scanf("%d", &t);
for(k=;k<=t;k++)
{
int a[]={}, b[]={}, c[]={}; /* 三个数组都要初始化 */
scanf("%s %s", s1, s2);
length_a=strlen(s1); length_b=strlen(s2);
for(i=; i<length_a; i++) a[i]=s1[length_a--i]-'';
for(i=; i<length_b; i++) b[i]=s2[length_b--i]-'';
rmax=reverse_add(a, b, c, length_a, length_b);
printf("Case %d:\n", k);
printf("%s + %s = ", s1, s2);
for(i=; i<rmax; i++) printf("%d", c[rmax--i]);
printf("\n");
if(t>&&k<t) printf("\n");
}
return ;
}
AC
最新文章
- jsPanel插件Option总结
- Python框架之Tornado(一)
- spring与redis简单整合
- Open-falon监控安装过程
- what is delta simulation time
- tab选项卡-jQuery
- crf 分词(待)
- OSChina 其中很重要的一类——RequestContext
- 忘记 mysql5.5.24 数据库 root 密码
- js 闭包和回调
- BAT Android工程师面试流程解析+还原最真实最完整的一线公司面试题
- JQuery拖拽元素改变大小尺寸
- SQLite数据库 简介、特点、优势、局限性及使用
- Java中返回类型方法名
- linux下mysql多实例安装
- Java NIO -- 缓冲区(Buffer)的数据存取
- BCG库的一个bug
- radio的取值
- 《C++ Primer (V4)》读书笔记
- show tables from information_schema/performance_schema/sys;
热门文章
- Structs复习 OGNL
- (转)JS之——解决IE6、7、8使用JSON.stringify报JSON未定义错误的问题
- py3 读入和写入csv,txt文件
- Java NIO Files
- 函数式编程语言(Functional Program Language)
- Redis安装完后redis-cli无法使用(redis-cli: command not found)已使用
- springcloud eureka.instance
- 【C++】正则表达式引擎学习心得
- STL::bitset
- 重建二叉树(python)