洛谷[P1601] A+B Problem(高精)
输入格式
分两行输入。a,b≤10^500a,b≤10^500
输出格式
输出只有一行,代表a+b的值
总结
getchar的使用有风险,可能无法妥善处理数据的读入何时终止。
1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 7 using namespace std; 8 9 int a[600], b[600], c[600], la, lb; 10 bool tag = false; 11 char s1[600], s2[600]; 12 13 inline void read(int & x) 14 { 15 x = 0; 16 int k = 1; 17 char c = getchar(); 18 while (!isdigit(c)) 19 if (c == '-') c = getchar(), k = -1; 20 else c = getchar(); 21 while (isdigit(c)) 22 x = (x << 1) + (x << 3) + (c ^ 48), 23 c = getchar(); 24 x *= k; 25 } 26 27 int main() 28 { 29 cin >> s1 >> s2; 30 int l1 = strlen (s1); 31 int l2 = strlen (s2); 32 for (int i = 1; i <= l1; ++i) 33 a[i] = s1[i - 1] - '0'; 34 for (int i = 1; i <= l2; ++i) 35 b[i] = s2[i - 1] - '0'; 36 la = l1; lb = l2; 37 reverse (a + 1, a + la + 1); 38 reverse (b + 1, b + lb + 1); 39 int len = max(la, lb), x = 0; 40 for (int i = 1; i <= len; ++i) 41 c[i] = a[i] + b[i] + x, 42 x = c[i] / 10, 43 c[i] %= 10; 44 if (x) c[++len] = 1; 45 while (c[len] == 0 && len > 1) --len; 46 for (int i = len; i >= 1; --i) 47 cout << c[i]; 48 return 0; 49 }

浙公网安备 33010602011771号