标签:
描述
给定任意位数长度的两个数,求它们数值之和。
输入
每个测试用例含有两个数,代表两个任意位数长度的数A,B.
输出
计算出这两个任意位数长度的数之和。
样例输入
样例输出
#include<iostream> #include<cstring> using namespace std; int la, lb; char a[1000000],b[1000000]; //数组补0 void add0(char q[],int t,int j) { int i; for(i = 0; i < j; i++) { q[t++] = ‘0‘; } q[t] = ‘\0‘; } int main() { int as, bs, diana, dianb, i, j; char *pa, *pb, ch; while(~scanf("%s %s",a,b)) { pa = strchr(a,‘.‘); pb = strchr(b,‘.‘); la = strlen(a); lb = strlen(b); if(pa == NULL) //如果输入的a是整数那么最后加小数点 { a[la] = ‘.‘; la = la+1; pa = strchr(a,‘.‘); } if(pb == NULL) //如果输入的a是整数那么最后加小数点 { b[lb] = ‘.‘; lb = lb+1; pb = strchr(b,‘.‘); } // if(pa!=NULL) // { diana = pa-a; dianb = pb-b; j = la-diana-lb+dianb; add0(b,lb,j); // } // if(pb!= NULL) // { // diana = pa-a; // dianb = pb-b; j = lb-dianb-la+diana; add0(a,la,j); // } strrev(a); strrev(b); la = strlen(a); lb = strlen(b); if(la>lb) add0(b,lb,la-lb); else if(la<lb) add0(a,la,lb-la);//printf("%s %s\n",a,b); j = 0; la = strlen(a); for(i = 0; i < la; i++) { if(a[i]==‘.‘) continue; ch = (a[i]+b[i]-96+j)%10+‘0‘; j = (a[i]+b[i]-96+j)/10; a[i] = ch; } a[i] = ‘\0‘; strrev(a); la = strlen(a); i = la-1; while(a[i]==‘0‘) { i--; } a[i+1] = ‘\0‘; if(a[i]==‘.‘) a[i] = ‘\0‘; if(j) printf("%d",j); printf("%s\n",a); } return 0; }
标签:
原文地址:http://www.cnblogs.com/jianqian/p/4984908.html