1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include <stdio.h> #include <string.h> int change(int a[], char b[]); void add(int a[], int b[], int alen, int blen); int c[25] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97}; int main() { char A[50], B[50]; int a[26], b[26], alen, blen; while(scanf("%s%s", A, B) != EOF) { //输入两个字符串并分别存储在A和B两个字符数组中 if(A[0] == '0' && B[0] == '0') //结果不输出,循环结束 break; alen = change(a, A); //字符数组A转换成整型数组a并且逆置,返回数组长度alen blen = change(b, B); //字符数组B转换成整型数组b并且逆置,返回数组长度blen if(alen > blen) add(a, b, alen, blen); else add(b, a, blen, alen); } } int change(int a[], char b[]) { //将字符数组转换成整型数组并且逆置 int j=0; for(int i = strlen(b) - 1; i >= 0; i--) if(b[i] != ',') { if(b[i-1] == ',' || i == 0) a[j++] = b[i] - '0'; else { a[j++] = (b[i-1] - '0') * 10 + (b[i] - '0'); i--; } } return j; //返回整型数组长度 } void add(int a[], int b[], int alen, int blen) { //相加 int i = 0; while(i < alen) { if(i < blen) a[i] += b[i]; if(a[i] >= c[i]) { a[i] -= c[i]; if(i == alen - 1) { a[i+1] = 1; alen++; } else a[i+1]++; } i++; } for(int j = alen - 1; j >= 0; j--) { //反向输出 if(j == 0) printf("%d", a[j]); else printf("%d,", a[j]); } printf("\n"); }
|