Problem:
Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.
#include <stdio.h> #include <math.h> int iResult[100] = {0}; void AddToResult(char *pch,int length) { int i = 0; for(i = 0;i < length;i++) { iResult[i+50] += *pch - 48; pch++; } } void main() { char digit[100][50]; int irow = 0,icol = 0; int i = 0; char ch; for(irow = 0;irow < 100;irow++) { for(icol = 0;icol < 50;icol++) { scanf("%c",&digit[irow][icol]); } scanf("%c",&ch); AddToResult(digit[irow],50); } for(i = 99; i > 0;i-- ) { if(10 < iResult[i]) { iResult[i-1] += iResult[i] / 10; iResult[i] = iResult[i] % 10; } } for(i = 0; i <= 90;i++ ) { if(0 < iResult[i]) { int j = 0; for(j = 0;j < 10;j++) { printf("%d",iResult[i+j]); } break; } } printf("\n"); }
Result:
5537376230
ProjectEuler_P13,布布扣,bubuko.com
原文:http://www.cnblogs.com/zhoueh1991/p/3712816.html