北大ACM1503 Integer Inquiry
Integer InquiryTime Limit: 1000MS Memory Limit: 10000K
Total Submissions: 27761 Accepted: 10775
Description
One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers.
``This supercomputer is great,'' remarked Chip. ``I only wish Timothy were here to see these results.'' (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.)
Input
The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative).
The final input line will contain a single zero on a line by itself.
Output
Your program should output the sum of the VeryLongIntegers given in the input.
Sample Input
123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0
Sample Output
370370367037037036703703703670
这道题目贡献了我10+次WA 就是因为没有考虑前导零的情况 我真心跪了!
#include <stdio.h> #include <string.h> #define N 105 int data[N][N]; int length = 0; int sum[N] = {0}; int StrlenOfThis(char *str) { char *s ; for(s = str; *s; s++) ; return (s-str); } void Convert(char str[]) { int i,j; for(j = 0,i = data[length][N-1]-1; i >=0; i--, j++) { data[length][j] = str[i]-'0'; } } void Sum() { int jinwei = 0,i,j,x; for(i = 0; i < length; i++) { for(j = 0; j < data[i][N-1] || jinwei > 0; j++) { x = sum[j] + data[i][j] + jinwei; jinwei = x / 10; sum[j] = x % 10; } jinwei = 0; } } void Show() { int i = N-1,j; while(sum[i]==0)i--; for(j = i; j >= 0; j--) printf("%d",sum[j]); printf("\n"); } int main() { char str[150]; int i; while(1) { scanf("%s",str); // 检测前导零的情况 for(i = 0; i < StrlenOfThis(str); i++) if(str[i] != '0')break; if(i == StrlenOfThis(str)) break; data[length][N-1] = StrlenOfThis(str); Convert(str); length++; memset(str,0,sizeof(str)); } if(length == 0) printf("0"); else{ Sum(); Show(); } return 1; }
补充:软件开发 , C++ ,