如用到bign类参见大整数加减乘除模板
#include <iostream> #include <string> #include <cstring> #include <cstdio> #include <cstdlib> #define N 10050 using namespace std; string s; int ans[N]; int main() { int i, j; while (cin>>s, s[0] != '0') { for (i = s.length() - 1, j = 0; i >= 0; i--, j++) ans[j] += (s[i] - '0'); } for (i = 0; i < N - 1; i++) { ans[i + 1] += ans[i] / 10; ans[i] %= 10; } i = N - 1; while (!ans[i] && i > 0) i--; while (i >= 0) cout<<ans[i--]; cout<<endl; return 0; }
#include <iostream> #include <string> #include <cstring> #include <algorithm> #include <cstdio> #include <cstdlib> #define N 550 using namespace std; int ans[N]; string s1, s2; int main() { int i, j, ls1, ls2, len;//tmp表示进位 while (cin>>s1>>s2) { ls1 = s1.length() - 1; ls2 = s2.length() - 1; len = ls1 + ls2; memset(ans, 0, sizeof(ans)); for (i = ls1; i >= 0; i--) for (j = ls2; j >= 0; j--) { ans[len - (i + j)] += (s1[i] - '0') * (s2[j] - '0'); } len++; for (i = 0; i < len; i++) { ans[i + 1] += ans[i] / 10; ans[i] = ans[i] % 10; } while (!ans[len] && len > 0)//去除前导0 len--; for (; len >= 0; len--) cout<<ans[len]; cout<<endl; } return 0; }
int main() { //ifstream cin("test.in"); bign a, b, c; char ch; bign d = INT_MAX; string sa, sb; while (cin>>sa>>ch>>sb) { cout<<sa<<' '<<ch<<' '<<sb<<endl; a = sa; b = sb; if (d < a) cout<<"first number too big"<<endl; if (d < b) cout<<"second number too big"<<endl; if (ch == '+') c = a + b; else c = a * b; cout<<c<<endl; if (d < c) cout<<"result too big"<<endl; } return 0; }
int main() { string sa, sb; bign a, b; int p, n, i; while (cin>>sa>>n) { p = 0; while (sa[p] != '.') p++; sa.erase(sa.begin() + p); a = sa; b = a ^ n; sb = b.to_str(); p = (5 - p) * n; for (i = b.length(); i < p; i++) sb += '0'; sb.insert(sb.begin() + p, '.'); p = 0; while (sb[p] == '0') p++; for (i = sb.length() - 1; i >= p; i--) cout<<sb[i]; cout<<endl; } return 0; }
10494 - If We Were a Child Again
int main() { //string sa, sb; bign a, b, c; char ch; while (cin>>a>>ch>>b) { if (ch == '/') c = a / b; else c = a % b; cout<<c<<endl; } return 0; }