poj 1503 Integer Inquiry (高精度运算)
题目链接:http://poj.org/problem?id=1503
思路分析: 基本的高精度问题,使用字符数组存储然后处理即可。
代码如下:
#include <iostream> #include <string> using namespace std; const int M = 100 + 10; char input[M]; int A[M], sum[M]; void Reverse(int A[], const char str[]) { int len = strlen(str); int index = 0; for (int i = len - 1; i >= 0; --i) A[index++] = str[i] - '0'; } int main() { int sumLen = 0; memset(sum, 0, sizeof(sum)); while (scanf("%s", input) != EOF) { int digitSum; if (strcmp(input, "0") == 0) break; memset(A, 0, sizeof(A)); Reverse(A, input); for (int i = 0 ; i < M; ++i) { digitSum = sum[i] + A[i]; sum[i] = digitSum % 10; sum[i+1] += digitSum/10; } } for (int i = M-1; sum[i] == 0; --i) sumLen = i; for(int i = sumLen-1; i >= 0; --i) printf("%d", sum[i]); printf("\n"); return 0; }