HDU - 1002 A + B Problem II
来源:互联网 发布:c语言入门看什么书 编辑:程序博客网 时间:2024/05/23 18:16
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
21 2112233445566778899 998877665544332211
Sample Output
Case 1:1 + 2 = 3Case 2:112233445566778899 + 998877665544332211 = 1111111111111111110
题解:很明显是大数相加,但是它全部都是正数那更容易了,输入之后全部反序变成数字,两个数组个个相加,把进位记录,差不多就是这样,不过要注意换行之类的
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int a[1010],b[1010],sum[1010];int len_a,len_b,len_sum;void changeInt(char str[],int num[],int *l){ *l = strlen(str) - 1; int index = 0; for(int i = *l;i >= 0 ;i--){ num[i] = str[index++] - '0'; }}void add(int a[],int b[],int s[]){ int c = 0; int m = 0; while(len_a>=m || len_b>=m){ if(len_a<m)a[m] = 0; if(len_b<m)b[m] = 0; s[m] = a[m]+b[m]+c; c = s[m]/10; if(s[m]>9){ s[m]=s[m]%10; } m++; } if(c) s[m] = c; len_sum = m - 1 + c;}void write(int num[],int l){ for(int i = l;i >= 0;i--)printf("%d",num[i]);}int main(){ int T;cin>>T;int count = 0; for(int i = 0; i < T ; i++){ char m[1010],n[1010]; if(count != T && count)printf("\n"); cin>>m>>n; changeInt(m,a,&len_a);changeInt(n,b,&len_b); add(a,b,sum); printf("Case %d:\n",++count); write(a,len_a); printf(" + "); write(b,len_b); printf(" = "); write(sum,len_sum); printf("\n"); } return 0;}
0 0
- HDU 1002 A + B Problem II
- HDU 1002A + B Problem II
- HDU 1002 - A + B Problem II
- HDU 1002 A + B Problem II
- HDU 1002 A + B Problem II
- HDU 1002 A + B Problem II
- HDU 1002 A + B Problem II
- HDU 1002 A + B Problem II
- hdu 1002 A+B Problem II
- HDU 1002 A + B Problem II
- hdu 1002 A + B Problem II
- HDU 1002 A + B Problem II (BigNums)
- hdu 1002 A + B Problem II
- hdu 1002 A + B Problem II
- hdu 1002 A + B Problem II
- HDU 1002 A + B Problem II
- HDU 1002 A + B Problem II
- Hdu---A + B Problem II---1002
- 你理解我的意思么?
- 中国顶级黑客的生意—---在商业利益与社会责任间两难抉择
- STL(十):迭代器适配器{(插入迭代器back_insert_iterator)、IO流迭代器(istream_iterator、ostream_iterator)}
- cocos2d-x 弹入、弹出效果(以菜单为例子)
- 项目准备
- HDU - 1002 A + B Problem II
- 指针高级的应用
- python 笔记一 numpy,pylab
- 【呆子谈营销】:说说那些热点事情之后的借势营销
- Linux下动态库(.so)和静态库(.a) 的区别
- SecureCRT进入全屏和退出全屏
- Car race game 归并排序求逆序数
- oracle 11g RMAN:Active Database Duplication for standby database 创建dg 命令解读
- 回车 换行符