标签:
用数组存好数据并按位进行加法运算
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002
#include<stdio.h> #include<math.h> #include<string.h> #include<iterator> #include<iostream> #include<algorithm> #include<set> using namespace std; int main() { int a1[1005],a2[1005]; char s1[1005],s2[1005]; int num,i,j,n,count,len1,len2; scanf("%d",&num); count=1; n=num; while(num--) { memset(a1,0,sizeof(a1)); memset(a2,0,sizeof(a2)); scanf("%s %s",s1,s2); len1=strlen(s1); len2=strlen(s2); int t=0; for(i=len1-1;i>=0;i--) a1[t++]=s1[i]-‘0‘;//注意从后往前存 t=0; for(i=len2-1;i>=0;i--) a2[t++]=s2[i]-‘0‘; for(i=0;i<1002;i++) { a1[i]+=a2[i]; if(a1[i]>=10) { a1[i]-=10; a1[i+1]++; } } printf("Case %d:\n",count++); printf("%s + %s = ",s1,s2); for(i=1001;i>=0;i--) if(a1[i]) break; for(j=i;j>=0;j--) printf("%d",a1[j]);//从后往前输出 printf("\n"); if(count!=n+1) printf("\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/lmqpt/p/4774451.html