UVa-Getting Started
AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 0. Getting Started
// 10055 - Hashmat the Brave Warrior #include <iostream> using namespace std; int main(void) { long a, b; // The input numbers are not greater than 2^32 while(cin >> a >> b) cout << (a>b ? a-b:b-a) << endl; // or vice versa return 0; } // 10071 - Back to High School Physics #include <iostream> using namespace std; int main(void) { int v, t; while(cin >> v >> t) cout << 2*v*t << endl; return 0; } // 10300 - Ecological Premium #include <iostream> using namespace std; int main(void) { int i, n, f, a, b, c, s; while(cin >> n) { while(n--) { cin >> f; s = 0; for(i=0; i<f; i++) { cin >> a >> b >> c; s += a*c; } cout << s << endl; } } return 0; } // 458 - The Decoder #include <cstdio> int main(void) { int c; while((c=getchar()) != EOF) if(c != '\n') putchar(c-7); else putchar('\n'); return 0; } // 494 - Kindergarten Counting Game #include <cstdio> #include <cctype> int main(void) { int c, ok=1, cnt=0; while((c=getchar()) != EOF) { if(c != '\n') { if(isalpha(c) && ok) { cnt++; ok = 0; } else if(isalpha(c)) { ok = 0; } else { ok = 1; } } else { printf("%d\n", cnt); ok = 1; cnt = 0; } } return 0; } // 414 - Machined Surfaces #include <cstdio> #define MAXN 26 int main(void) { char s[MAXN]; int i, j, n, k, min, sum; while(scanf("%d%*c", &n) && n) { min = MAXN; sum = 0; for(i=0; i<n; i++) { k = 0; gets(s); for(j=0; j<MAXN; j++) if(s[j] == ' ') k++; if(k < min) min = k; sum += k; } printf("%d\n", sum-min*n); } return 0; } // 490 - Rotating Sentences #include <cstdio> #include <cstring> #define MAXN 101 int main(void) { int i, j, r=0, maxlen=0; int len[MAXN]; char s[MAXN][MAXN]; while(gets(s[r]) != NULL) { len[r] = strlen(s[r]); if(len[r] > maxlen) maxlen = len[r]; r++; } for(j=0; j<maxlen; j++){ for(i=r-1; i>=0; i--) if(j < len[i]) printf("%c", s[i][j]); else printf(" "); printf("\n"); } return 0; } // 445 - Marvelous Mazes #include <cstdio> #include <cctype> int main(void) { char c; int n = 0; while((c=getchar()) != EOF) { if(isdigit(c)) n += c-'0'; else if(c=='!' || c=='\n') putchar('\n'); else if(c=='b') while(n) {putchar(' '); n--;} else while(n) {putchar(c); n--;} } return 0; } // 488 - Triangle Wave #include <iostream> using namespace std; int main(void) { int i, j, n, A, F; while(cin >> n) { while(n--) { cin >> A >> F; while(F--) { for(i=1; i<=A; i++) { for(j=1; j<=i; j++) cout << i; cout << endl; } for(i=A-1; i>=1; i--) { for(j=1; j<=i; j++) cout << i; cout << endl; } if(F != 0) // !!! cout << endl; } if(n != 0) // !!! cout << endl; } } return 0; } // 489 - Hangman Judge #include <iostream> #include <cstdio> #include <cstring> using namespace std; int main(void) { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); char word[100], guess[100]; int i, n, len, flag, match, strokes, alpha[26], vis[26]; while(cin>>n && n!=-1) { flag = 1; len = match = strokes = 0; memset(alpha, 0, sizeof(alpha)); memset(vis, 0, sizeof(vis)); cout << "Round " << n << endl; cin >> word >> guess; for(i=0; word[i]; i++) alpha[word[i]-'a'] = 1; for(i=0; i<26; i++) len += alpha[i]; for(i=0; guess[i]; i++) { if(alpha[guess[i]-'a'] && !vis[guess[i]-'a']) { match++; vis[guess[i]-'a'] = 1; } if(!alpha[guess[i]-'a']) strokes++; if(match == len) { cout << "You win." << endl; flag = 0; break; } if(strokes == 7) { cout << "You lose." << endl; flag = 0; break; } } if(flag) cout << "You chickened out." << endl; } return 0; } // 694 - The Collatz Sequence #include <iostream> #include <cstdio> using namespace std; int main(void) { int terms, Case=1; long t, A, L; // 避免中间结果溢出导致无限循环 while(cin>>t>>L && (t!=-1||L!=-1)) { A = t; terms = 1; while(A != 1) { if(A%2) A=3*A+1; else A=A/2; if(A > L) break; // larger than limit ! else terms++; } printf("Case %d: A = %ld, limit = %ld, number of terms = %d\n", Case++, t, L, terms); } return 0; } // 457 - Linear Cellular Automata #include <iostream> #include <cstring> using namespace std; int main(void) { int i, j, n, t, p; int DNA[10], dish[42]; cin >> n; while(n--) { for(i=0; i<10; i++) cin >> DNA[i]; memset(dish, 0, sizeof(dish)); dish[20] = 1; cout<<" . "<<endl; for(i=1; i<50; i++) { p = 0; for(j=1; j<=40; j++) { t = dish[j]; dish[j] = DNA[p + dish[j] + dish[j+1]]; p = t; switch(dish[j]) { case 0: cout << ' '; break; case 1: cout << '.'; break; case 2: cout << 'x'; break; case 3: cout << 'W'; break; } } cout << endl; } if(n != 0) cout << endl; } return 0; }