HDU1002
A + B Problem II
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 434325 Accepted Submission(s): 84533
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
与1001不同,这次是1000单位内长度的整数加法运算,java可以用biginteger,用法可百度
C/C++可以将整数加法定义用代码实现,即相同位的数相加,得10进1到前一位数中,不难发现,同位数相加不管怎么加都不可能有20的,所以在最高位相加如果大于10,就提前输出一个1就行
数据录入:
因为需要计算的长度非常长,并且需要用整数加法定义,需要把一大串数字拆分成独立的个位数才好操作,因此可以使用字符串-字符数组进行操作。
PS(字符的‘1’与数字的1在ASCII码中相差48,
即a = '1'; b = 1; a == b + 48
(代码并不规范))
细节补充:
输入的两个数长度并不一定相同,所以需要通过操作将短的字符串变成和长的字符串一样长,前面用0补足即可
例:
123 + 233333 → 000123 + 233333
写题遇到的问题:
第一次没有考虑到输入是开头带0的情况(如0000123,实质上就是123),导致在字符串长度对比处出现问题,需注意
第二次没考虑到输入的其中一个值就是0的情况(00000同,下略)如果输入就是一个0 ,在“我自己的代码情况下”无法输出 0 + 123 = 123
第三次是进位问题,需要仔细考虑,第一次进位成功后第二次判断是否进位需要加上进位的值等等
第四次 审题!审题!审题!output必须要仔细看,明确写了
Output a blank line between two test cases.,在两行输出中空一行,所以在最后一行是不需要再空行的
科普:
Presentation Error
表示格式不正确,答案已经非常接近了,一般是多个空行或者空格之类,据说过多的注释也会导致这种问题出现
代码:
#include<stdio.h>
#include<string.h>
int main(){
int n;
scanf("%d",&n);
for(int count=1;count<=n;count++){
char a[1010];
char b[1010];
int sum[1010];
int minlen=0;
int maxlen=0;
int temlen = 0;
scanf("%s %s",&a,&b);
// printf("%d\n%d",strlen(a),strlen(b));
// for(int i=0;i<=5;i++){
// printf("%d",a[i]-48);
// }
int lena = strlen(a);
int lenb = strlen(b);
int pos=0;
for(int i=0;i<=lena;i++){
// printf("a[%d] = %d\n",i,a[i]-48);
if(a[i]!='0'){
break;
}
pos++;
}
// printf("位移数据\n");
for(int i = pos;i<lena;i++){
a[i-pos]=a[i];
// printf("a[%d] = %d\n",i-pos,a[i-pos]-48);
}
lena = lena - pos;
if(a[0]=='0'){
lena++;
}
// printf("lena = %d\n",lena);
pos = 0;
for(int i=0;i<=lenb;i++){
// printf("b[%d] = %d\n",i,b[i]-48);
if(b[i]!='0'){
break;
}
pos++;
}
// printf("位移数据\n");
for(int i = pos;i<lenb;i++){
b[i-pos]=b[i];
// printf("b[%d] = %d\n",i-pos,b[i-pos]-48);
}
lenb = lenb - pos;
if(b[0]=='0'){
lenb++;
}
// printf("lenb = %d\n",lenb);
printf("Case %d:\n",count);
for(int i = 0;i<lena;i++){
if(a[0]=='0'||a[0]==0){
printf("0");
break;
}
printf("%d",a[i]-48);
}
printf(" + ");
for(int i = 0;i<lenb;i++){
if(b[0]=='0'||b[0]==0){
printf("0");
break;
}
printf("%d",b[i]-48);
}
printf(" = ");
maxlen = lena;
if(lena>lenb){
int j = 0;
minlen = lenb;
maxlen = lena;
temlen = lena-lenb;
char temp[1001];
for(int i =0;i<1001;i++){
temp[i]='0';
}
for(int i = temlen;i < maxlen; i++){
temp[i] = b[j++];
}
// printf("数组变换:");
for(int i = 0;i<maxlen;i++){
b[i] = temp[i];
// printf("%d",b[i]-48);
}
// printf("\n");
}else if(lena<lenb){
int j = 0;
minlen = lena;
maxlen = lenb;
temlen = lenb-lena;
char temp[1001];
for(int i =0;i<1001;i++){
temp[i]='0';
}
for(int i = temlen;i < maxlen; i++){
temp[i] = a[j++];
}
// printf("数组变换:");
for(int i = 0;i<maxlen;i++){
a[i] = temp[i];
// printf("%d",a[i]-48);
}
// printf("\n");
}
// printf("maxlen = %d",maxlen);
int plus = 0;
for(int i = maxlen-1 ; i>=0 ;i--){
if(a[i]+b[i]-96+plus>9&&i!=0){
sum[i]=a[i]+b[i]-96+plus-10;
plus = 1;
}
else if(a[i]+b[i]-96+plus<=9&&i!=0){
sum[i]=a[i]+b[i]-96+plus;
plus = 0;
}
else if(i==0){
if(a[0]+b[0]+plus-48-48>9){
printf("1");
sum[0]=a[0]+b[0]+plus-96-10;
}else{
sum[0]=a[0]+b[0]+plus-96;
}
for(int k=0;k<maxlen;k++){
printf("%d",sum[k]);
}
}
// printf("a[%d] = %d\n",i,a[i]-48);
// printf("b[%d] = %d\n",i,b[i]-48);
// printf("sum[%d] = %d\n",i,sum[i]);
}
printf("\n\n");
}
return 0;
}
后记:
好久没有自己独立做题AC的成就感了,1001算是对DEV C++的熟悉+练手所以没什么感觉,就自己写题过程中就感觉其实有很多重复代码区,完全可以写成函数在视觉上进一步简化,但是使用函数调用数组完全没实际写过,印象中似乎是要用到指针的应用,待会儿C基础视频看完就拿这题当指针+函数的训练题来做。