# ACM-ICPC 寒假练习 03

## UVaOJ 424

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41  #include #include #include using namespace std; const int MAX = 10240; int nLen; int pData[MAX]; int main() { nLen = 1; memset(pData, 0, sizeof(pData)); string x; while(cin >> x) { if(x != "0") { while(x[0] == '0') { x = x.substr(1, x.length() - 1); } nLen = max(nLen, (int)x.length()); for(int i = 1; i <= x.length(); i++) { pData[i] += x[x.length() - i] - '0'; pData[i + 1] += pData[i] / 10; pData[i] %= 10; } while(pData[nLen + 1]) { nLen++; } } else { for(int i = nLen; i >= 1; i--) { cout << pData[i]; } cout << endl; nLen = 1; memset(pData, 0, sizeof(pData)); } } return 0; } 

## UVaOJ 10106

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37  #include #include #include using namespace std; const int MAX = 10240; int nLen; int pData[MAX]; int main() { string x, y; while(cin >> x >> y) { if(x.length() < y.length()) { swap(x, y); } nLen = x.length() + y.length(); memset(pData, 0, sizeof(pData)); for(int i = 1; i <= y.length(); i++) { for(int j = 1; j <= x.length(); j++) { pData[i + j - 1] += (x[x.length() - j] - '0') * (y[y.length() - i] - '0'); pData[i + j] += pData[i + j - 1] / 10; pData[i + j - 1] %= 10; } } while(nLen && !pData[nLen]) { nLen--; } if(nLen == 0) { nLen = 1; } for(int i = nLen; i >= 1; i--) { cout << pData[i]; } cout << endl; } return 0; } 

## UVaOJ 465

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24  #include #include #include using namespace std; const int MAX_INT = 2147483647; int main() { char dwOpt; string x, y; while(cin >> x >> dwOpt >> y) { cout << x << " " << dwOpt << " " << y << endl; double a, b; a = atof(x.c_str()); b = atof(y.c_str()); if(a > MAX_INT) { cout << "first number too big" << endl; } if(b > MAX_INT) { cout << "second number too big" << endl; } if(dwOpt == '+' && a + b > MAX_INT) { cout << "result too big" << endl; } if(dwOpt == '*' && a * b > MAX_INT) { cout << "result too big" << endl; } } } 

## UVaOJ 748

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67  #include #include #include using namespace std; const int MAX = 10240; int nLen, nDot; int pData[MAX]; string Solve(string x, string y); int main() { string x; int y; while(cin >> x >> y) { while(x[x.length() - 1] == '0') { x = x.substr(0, x.length() - 1); } nDot = x.length() - x.find('.') - 1; nDot *= y; x = x.substr(0, x.find('.')) + x.substr(x.find('.') + 1, x.length() - x.find('.') - 1); string z = "1"; for(int i = 1; i <= y; i++) { z = Solve(z, x); } if(z.length() <= nDot) { cout << "."; for(int i = z.length(); i < nDot; i++) { cout << "0"; } cout << z << endl; } else { for(int i = 0; i < z.length() - nDot; i++) { cout << z[i]; } cout << "."; for(int i = z.length() - nDot; i < z.length(); i++) { cout << z[i]; } cout << endl; } } return 0; } string Solve(string x, string y) { if(x.length() < y.length()) { swap(x, y); } nLen = x.length() + y.length(); memset(pData, 0, sizeof(pData)); for(int i = 1; i <= y.length(); i++) { for(int j = 1; j <= x.length(); j++) { pData[i + j - 1] += (x[x.length() - j] - '0') * (y[y.length() - i] - '0'); pData[i + j] += pData[i + j - 1] / 10; pData[i + j - 1] %= 10; } } while(nLen && !pData[nLen]) { nLen--; } if(nLen == 0) { nLen = 1; } string z = ""; for(int i = 1; i <= nLen; i++) { z = (char)(pData[i] + '0') + z; } return z; } 

## UVaOJ 10494

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55  #include #include using namespace std; const int MAX = 10240; int nLen; int pData[MAX], pAns[MAX]; string Solve(string x, long long y, char dwOpt); int main() { char dwOpt; string x; long long y; while(cin >> x >> dwOpt >> y) { cout << Solve(x, y, dwOpt) << endl; } return 0; } string Solve(string x, long long y, char dwOpt) { memset(pData, 0, sizeof(pData)); memset(pAns, 0, sizeof(pAns)); nLen = x.length(); for(int i = 1; i <= x.length(); i++) { pData[i] = x[x.length() - i] - '0'; } long long d = 0; for(int i = nLen; i >= 1; i--) { d = d * 10 + pData[i]; pAns[i] = d / y; d %= y; } while(nLen && pAns[nLen] == 0) { nLen--; } if(nLen == 0) { nLen = 1; } string z = ""; if(dwOpt == '/') { for(int i = 1; i <= nLen; i++) { z = (char)(pAns[i] + '0') + z; } } else { while(d) { z = (char)(d % 10 + '0') + z; d /= 10; } if(z == "") { z = "0"; } } return z; }