Project Euler 13 Large sum


题意:计算出以下一百个50位数的和的前十位数字。


/*************************************************************************
    > File Name: euler013.c
    > Author:    WArobot 
    > Blog:      http://www.cnblogs.com/WArobot/ 
    > Created Time: 2017年06月25日 星期日 10时55分56秒
 ************************************************************************/

#include <stdio.h>
#include <inttypes.h>
#include <string.h>

#define max(a,b) ((a) > (b) ? (a) : (b))

int32_t main() {
	char s[55];
	int32_t ans[5010] = {0};
	for(int32_t t = 0 ; t < 100 ; t++){
		scanf("%s",s);
		int32_t len = strlen(s);
		ans[0] = max( ans[0] , len );			// ans[0]记录最大位数
		for(int32_t i = len-1 ; i >= 0 ; i--){	// 倒序加到ans[]里
			ans[ len - i ] += s[i] - '0';
		}
	}
	for(int32_t i = 1 ; i <= ans[0] ; i++){
		if( ans[i] >= 10 ){                            // 控制进位
			ans[ i + 1 ] += ans[i] / 10;
			ans[i] %= 10;
			if( ans[0] < i + 1 )	ans[0] = i + 1;
		}
	}
	for(int32_t i = ans[0] ; i >= ans[0] - 9 ; i--){
		printf("%d",ans[i]);
	}
	puts("");
	return 0;
}
原文地址:https://www.cnblogs.com/WArobot/p/7076368.html