ACM-ICPC 寒假练习 01

UVa 10055

  1 2 3 4 5 6 7 8 9 10 11 12 13 14  #include using namespace std; int main() { long long x, y; while(cin >> x >> y) { if(x > y) { swap(x, y); } cout << y - x << endl; } return 0; } 

UVa 10071

  1 2 3 4 5 6 7 8 9 10 11  #include using namespace std; int main() { int x, y; while(cin >> x >> y) { cout << x * y * 2 << endl; } return 0; } 

UVa 10300

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20  #include using namespace std; int main() { int T, N, x, y, z; cin >> T; for(int i = 1; i <= T; i++) { int ans = 0; cin >> N; for(int j = 1; j <= N; j++) { cin >> x >> y >> z; ans += x * z; } cout << ans << endl; } } 

UVa 458

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16  #include #include using namespace std; int main() { string x; while(getline(cin, x)) { for(int i = 0; i < x.length(); i++) { cout << (char)(x[i] - 7); } cout << endl; } return 0; } 

UVa 494

  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  #include #include using namespace std; int main() { string x, y; while(getline(cin, x)) { y = ""; int ans = 0; for(int i = 0; i < x.length(); i++) { if(x[i] >= 'A' && x[i] <= 'Z' || x[i] >= 'a' && x[i] <= 'z') { y += x[i]; } else { if(y != "") { ans++; y = ""; } } } cout << ans << endl; } return 0; } 

UVa 414

  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  #include #include using namespace std; int main() { int N; string x; while(cin >> N) { cin.get(); if(!N) { break; } int nTotal = 0, nMin = 2147483647; for(int i = 1; i <= N; i++) { int nTmp = 0; getline(cin, x); for(int j = 0; j < x.length(); j++) { if(x[j] == ' ') { nTmp++; } } nTotal += nTmp; nMin = min(nMin, nTmp); } cout << nTotal - nMin * N << endl; } return 0; } 

UVa 490

  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  #include #include using namespace std; const int MAX = 128; string strTmp[MAX]; int main() { int nPos = 0, nLen = 0; while(getline(cin, strTmp[++nPos])) { nLen = max(nLen, (int)strTmp[nPos].length()); } for(int j = 0; j < nLen; j++) { for(int i = nPos - 1; i >= 1; i--) { if(j < strTmp[i].length()) { cout << strTmp[i][j]; } else { cout << " "; } } cout << endl; } return 0; } int max(int x, int y) { return x > y ? x : y; } 

UVa 445

  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  #include #include using namespace std; string x; int main() { while(getline(cin, x)) { int nPos = 0; while(nPos < x.length()) { int nCnt = 0; if(x[nPos] == '!') { cout << endl; } while(x[nPos] >= '0' && x[nPos] <= '9') { nCnt += x[nPos] - '0'; nPos++; } for(int i = 1; i <= nCnt; i++) { if(x[nPos] == 'b') { cout << " "; } else { cout << x[nPos]; } } nPos++; } cout << endl; } return 0; } 

UVa 488

  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  #include using namespace std; void Tri(int x); int main() { int T, N, M; cin >> T; for(int i = 1; i <= T; i++) { cin >> N >> M; for(int j = 1; j <= M; j++) { Tri(N); if(j != M) { cout << endl; } } if(i != T) { cout << endl; } } } void Tri(int x) { for(int i = 1; i <= x - 1; i++) { for(int j = 1; j <= i; j++) { cout << i; } cout << endl; } for(int i = 1; i <= x; i++) { cout << x; } cout << endl; for(int i = x - 1; i >= 1; i--) { for(int j = i; j >= 1; j--) { cout << i; } cout << endl; } } 

UVa 489

  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  #include #include #include using namespace std; const int MAX = 32; bool pVisited[MAX]; int main() { int N; string x, y; while(cin >> N) { if(N == -1) { break; } bool bFlag = true; int nCnt = 0, nTmp = 0, nWrong = 0; memset(pVisited, false, sizeof(pVisited)); cout << "Round " << N << endl; cin >> x >> y; for(int i = 0; i < x.length(); i++) { if(!pVisited[x[i] - 'a']) { pVisited[x[i] - 'a'] = true; nCnt++; } } for(int i = 0; i < y.length(); i++) { if(pVisited[y[i] - 'a']) { pVisited[y[i] - 'a'] = false; nTmp++; } else { nWrong++; } if(nTmp == nCnt && nWrong < 7) { bFlag = true; break; } if(nTmp != nCnt && nWrong >= 7) { bFlag = false; break; } } if(bFlag) { if(nTmp == nCnt) { cout << "You win." << endl; } else { cout << "You chickened out." << endl; } } else { cout << "You lose." << endl; } } return 0; } 

UVa 694

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21  #include int main() { long long A, L, nCase = 0; while(scanf("%lld%lld", &A, &L) != EOF) { if(A == -1 && L == -1) { break; } printf("Case %lld: A = %lld, limit = %lld, number of terms = ", ++nCase, A, L); long long nCnt = 0; while(A <= L) { nCnt++; if(A == 1) { break; } if(A & 1) { A = 3 * A + 1; } else { A >>= 1; } } printf("%lld\n", nCnt); } return 0; } 

UVa 457

  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 using namespace std; const int MAX = 64; int pData[MAX], pDish[MAX][2]; int main() { int T; cin >> T; for(int i = 1; i <= T; i++) { memset(pDish, 0, sizeof(pDish)); pDish[20][0] = 1; for(int j = 0; j < 10; j++) { cin >> pData[j]; } for(int j = 1; j <= 50; j++) { for(int k = 1; k <= 40; k++) { if(pDish[k][0] == 0) { cout << " "; } if(pDish[k][0] == 1) { cout << "."; } if(pDish[k][0] == 2) { cout << "x"; } if(pDish[k][0] == 3) { cout << "W"; } } for(int k = 1; k <= 40; k++) { int nCnt = pDish[k - 1][0] + pDish[k][0] + pDish[k + 1][0]; pDish[k][1] = pData[nCnt]; } for(int k = 1; k <= 40; k++) { pDish[k][0] = pDish[k][1]; } cout << endl; } if(i != T) { cout << endl; } } return 0; }