样例输出
样例一: 100000000000000000000000000000000000000000000000000000.0000000000000000000000000000000000000111111111000000000000000000 样例二: 7 样例三: 5.9 样例四: 10.00099999999999999999999 以为很简单,结果花了很长时间,而且代码也特别的不好,这种大数题目,就随着自己心意去模拟就好,还要够细心
代码:
#include<iostream> #include<algorithm> #include<string.h> #include<cstdio> using namespace std; char a[210]={0},b[210]={0},c[210]={0},flag; int main() { char t1[210]={0},t2[210]={0}; int cc=0,i,j,x=0,y=0,lena,lenb,len,lent1,lent2; /* FILE *fp; fp = fopen("c://lanqiao.txt","r"); fscanf(fp,"%s",a); fscanf(fp,"%s",b); fclose(fp); puts(a); puts(b);*/ gets(a); gets(b); for (i=0; i<strlen(a); i++) { if (a[i]== '.') { x = i; break; } } for (i=0; i<strlen(b); i++) { if (b[i] == '.') { y = i; break; } } if (x!= 0 && y!=0) //都是小数 { strcpy(t1,a+x); strcpy(t2,b+y); lent1 = strlen(t1); lent2 = strlen(t2); lent1>lent2 ? len = lent1 : len =lent2; for (i = len-1; i>0; i--) { if (t1[i] == 0) t1[i] = '0'; if (t2[i] == 0) t2[i] = '0'; c[cc] += t1[i]-'0' + (t2[i]-'0'); if (c[cc] >= 10) { c[cc] %= 10; c[cc+1] = 1; } cc++; } if (c[cc] == 1) { c[cc+1] = 1; } c[cc] = '.'; //小数点位置 cc++; for (i=x-1,j=y-1; i>=0&&j>=0; i--,j--) { c[cc] += a[i]-'0' + (b[j]-'0'); if (c[cc] >= 10) { c[cc] %= 10; c[cc+1] = 1; } cc++; } while (i != -1)//a数组还有没计算完 { c[cc] += a[i] -'0'; if (c[cc] >= 10) { c[cc] %= 10; c[cc+1] = 1; } cc++; i--; } while (j != -1) { c[cc] += b[j] - '0'; if (c[cc] >= 10) { c[cc] %= 10; c[cc+1] = 1; } cc++; j--; } if (c[cc]==0) //没有进位 cc--; for (i=cc; i>=0; i--) { if (c[i] != '.') c[i] += '0'; cout<<c[i]; } } else if (x==0 && y==0)//都是整数 { lena = strlen(a); lenb = strlen(b); for (i= lena-1,j = lenb -1; i>=0&&j>=0; i--,j--) { c[cc] += a[i]-'0' +(b[i]-'0'); if (c[cc] >=10) { c[cc] %= 10; c[cc+1] = 1; } cc++; } while (i != -1)//a数组还有没计算完 { c[cc] += a[i] -'0'; if (c[cc] >= 10) { c[cc] %= 10; c[cc+1] = 1; } cc++; i--; } while (j != -1) { c[cc] += b[j] - '0'; if (c[cc] >= 10) { c[cc] %= 10; c[cc+1] = 1; } cc++; j--; } if (c[cc] == 0) cc--; for (i=cc; i>=0; i--) { if (c[i] != '.') c[i] += '0'; cout<<c[i]; } } else if ((x==0&&y!=0) || (x!=0&&y==0)) //只有1个有小数点 { if (x!=0) //a数组是小数 { strcpy(t1,a); a[x] = '\0'; lena = strlen(a); lenb = strlen(b); for (i= lena-1,j = lenb -1; i>=0&&j>=0; i--,j--) { c[cc] += a[i]-'0' +(b[i]-'0'); if (c[cc] >=10) { c[cc] %= 10; c[cc+1] = 1; } cc++; } while (i != -1)//a数组还有没计算完 { c[cc] += a[i] -'0'; if (c[cc] >= 10) { c[cc] %= 10; c[cc+1] = 1; } cc++; i--; } while (j != -1) { c[cc] += b[j] - '0'; if (c[cc] >= 10) { c[cc] %= 10; c[cc+1] = 1; } cc++; j--; } strcpy(t1,t1+x); printf("%s",t1); } else { strcpy(t1,b); b[y] = '\0'; lena = strlen(a); lenb = strlen(b); for (i= lena-1,j = lenb -1; i>=0&&j>=0; i--,j--) { c[cc] += a[i]-'0' +(b[j]-'0'); if (c[cc] >=10) { c[cc] %= 10; c[cc+1] = 1; } cc++; } while (i != -1)//a数组还有没计算完 { c[cc] += a[i] -'0'; if (c[cc] >= 10) { c[cc] %= 10; c[cc+1] = 1; } cc++; i--; } while (j != -1) { c[cc] += b[j] - '0'; if (c[cc] >= 10) { c[cc] %= 10; c[cc+1] = 1; } cc++; j--; } } if (c[cc] == 0) cc--; for (i=cc; i>=0; i--) { if (c[i] != '.') c[i] += '0'; cout<<c[i]; } strcpy(t1,t1+y); printf("%s",t1); } return 0; }