Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
大小为102的数组就OK100 个 100位的数字相加 最多有102位,所以最后存储结果的数组只要102就OK啦~ 极限情况: 100 × (10e100 -1 ) = 10e102 - 100 所有数据规模都如题中所述,只要注意处理以0开始的数字就好了~ 代码:http://ideone.com/UEsEP #include <stdio.h> #include <string.h> #include <assert.h> char result[102]; char a[100][101]; // Attention: one more space should be left for \0!!! int na=0; void read_data(){ na = 0; while(1){ scanf("%s", a[na]); if( a[na][0] == '0' && a[na][1] == 0 ){ return; }else{ na++; } } } int get_value(char al[], int pos){ int len = strlen( al ); //printf("len of this array is %d\n", len); if( len - pos - 1 >= 0 ){ return al[len-pos-1] - '0'; }else{ //printf("return 0 at pos=%d\n", pos); return 0; } } void calculation(){ int i=0; int j=0; int car=0; for( i=0; i<102; i++ ){ int sum = 0; //printf(" na = %d\n ", na); for (j=0; j<na; j++){ int value = get_value(a[j], i); //if(i>100) printf(" the value of j=%d, at i=%d\n is %d", j, i, value); sum += value; } //printf("sum = %d\n", sum); sum += car; int temp = sum; sum=temp%10; car = temp/10; result[i] = sum + '0'; } assert( car == 0 ); } void output(){ int i=0; int j=0; for(i=101; i>=0; i--){ if( result[i] != '0' ){ break; } } if( result[i] == '0' ){ putchar( '0' ); }else{ for(j=i; j>=0; j--){ putchar( result[j] ); } } } int main(void) { read_data(); calculation(); output(); return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator