hdu 1002 A+B Problem II
2012-10-10 18:28
369 查看
题目链接:
题目大意:将两个加数用字符串表示,按位相加,逢十进一。
代码如下:
题目大意:将两个加数用字符串表示,按位相加,逢十进一。
代码如下:
#include<iostream> #include<cstdio> #include<cstring> #include<stdlib.h> using namespace std; int n; char s1[1100],s2[1100]; int len1,len2; int main(void) { int i,j,k,p; int flag; scanf("%d",&n); for(i=1;i<=n;i++) { int a[1100]={0}; flag=0; printf("Case %d:\n",i); scanf("%s%s",s1,s2); len1=strlen(s1); len2=strlen(s2); j=len1-1; k=len2-1; p=0; printf("%s + %s = ",s1,s2); while(j>=0&&k>=0) { if(a[p]+(s1[j]-'0')+(s2[k]-'0')>=10)//按位相加,逢十进一 { a[p]=a[p]+(s1[j]-'0')+(s2[k]-'0')-10; a[p+1]++; } else a[p]=a[p]+(s1[j]-'0')+(s2[k]-'0'); p++; j--; k--; } if(j>=0) { for(int t=j;t>=0;t--) { a[p]=a[p]+(s1[t]-'0'); p++; } } else if(k>=0) { for(int t=k;t>=0;t--) { a[p]=a[p]+(s2[t]-'0'); p++; } } else if(a[p]!=0)//两个位数相同的两个数相加后最高位大于10的情况 p++; for(int t=p-1;t>=0;t--) { if(a[t]==0&&flag==0) continue; else { flag=1; printf("%d",a[t]); } } printf("\n"); if(i!=n) printf("\n"); } system("Pause"); return 0; }
相关文章推荐
- 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
- hdu1002——A + B Problem II
- hdu1002 大大大整数加法 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
- hdu1002 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