Integer Inquiry
1 #include<stdio.h> 2 #include<string.h> 3 4 char a[105][200] ; 5 char sum[200] ; 6 7 void add (char temp[] ) { 8 9 int rem = 0; 10 int k ; 11 int i ; 12 for (i = 0 ; temp[i] != '\0' ; i++ ) { 13 if ( ( k = temp[i] - '0' + sum[i] - '0' + rem ) >= 10 ) { 14 rem = 1 ; 15 sum[i] = '0' + k - 10; 16 } 17 else { 18 rem = 0 ; 19 sum[i] = '0' + k ; 20 } 21 } 22 if ( rem == 1) 23 sum[i] = '1' ; 24 for ( k = strlen (sum) - 1 ; sum[k] == 0 && k >= 0; k-- ) ; 25 if ( k == -1) 26 sum[k + 2] = '\0' ; 27 else 28 sum[k + 1] = '\0' ; 29 30 } 31 32 int main () { 33 // freopen ("a.txt" ,"r" , stdin ) ; 34 int n ; 35 int ls , li ; 36 int j ; 37 while ( gets(a[0]) != NULL ) { 38 n = 1 ; 39 while ( strcmp(a[n - 1] , "0") != 0 ) { 40 gets (a[n++]) ; 41 } 42 strcpy (sum , a[0] ) ; 43 strrev (sum) ; 44 45 for (int i = 1 ; i < n; i++ ) { 46 li = strlen (a[i]) ; 47 ls = strlen (sum) ; 48 strrev (a[i]) ; 49 if ( li > ls ) { 50 for (j = ls ; j < li ; j++ ) { 51 sum[j] = '0' ; 52 } 53 sum[j] = '\0' ; 54 } 55 else { 56 for (j = li ; j < ls ; j++ ) { 57 a[i][j] = '0' ; 58 } 59 a[i][j] = '\0' ; 60 } 61 add ( a[i] ) ; 62 } 63 strrev (sum) ; 64 puts (sum) ; 65 } 66 return 0; 67 }