發表文章

目前顯示的是有「ACM」標籤的文章

Q686: Goldbach's Conjecture(II)

//Q686 Goldbach's Conjecture(II) //Accepted 2008-04-21 13:35:20 #include <iostream> #include <cmath> using namespace std; int main(void) { int n = 0, nt; while(cin >> n) { if (n == 0) break; int ct = 0, count = 0; nt = n; int *prime = new int [n]; //get prime for(int i = 2; i <= nt; i++) { bool isprime = true; for(int j = 2; j <= sqrt(i) && isprime; j++) isprime = i % j != 0; if (isprime) prime[ct++] = i; } for(int i = 0; i < ct; i++) { int becheck = n - prime[i]; for(int j = 0; j < ct; j++) if (becheck == prime[j] && prime[i] >= prime[j]) { count++; break; } } cout << count << endl; ...

Q445: Marvelous Mazes

//Q445 Marvelous Mazes //Accepted 2008-04-20 11:51:13 #include <iostream> #include <string> using namespace std; int main(void) { int repeat = 0; string data; while(getline(cin, data)) { for(int i = 0; i < data.length(); i++) { if (data[i] >= '0' && data[i] <= '9') { repeat += data[i] - 48; continue; } if (data[i] == 'b') { for(int j = 0; j < repeat; j++) cout << " "; repeat = 0; } if (data[i] == '!') cout << "\n"; else { for(int j = 0; j < repeat; j++) cout << data[i]; repeat = 0; ...

Q412: Pi

//Q412 Pi //Accepted 2008-04-20 11:18:00 #include <iostream> #include <iomanip> #include <cmath> #include <stdio.h> using namespace std; int gcd(int, int); int p[32768]; int main(void) { int t = 0, n[50]; double pi, tt, bp = 0; while(cin >> t) { bp = 0; if (t == 0) exit(0); for(int i = 0; i < t; i++) cin >> n[i]; for(int i = 2; i <= t; i++) for(int j = 1; j <= i - 1; j++) if (n[j - 1] > n[i - 1]) { if (gcd(n[j - 1], n[i - 1]) == 1) bp++; } else { if (gcd(n[i - 1], n[j - 1]) == 1) bp++; } tt = t * (t - 1) / 2; if (bp == 0) cout << "No estimate for this data set." << endl; else { pi = sqrt(...

Q256: Quirksome Squares

//Q256 Quirksome Squares //Accepted 2008-04-19 12:29:19 #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main(void) { int n, c, k = 0; while(cin >> n) { c = static_cast<int>(pow(10.0, n/2)); for (int i = 0; i < c * c - 1; i++) if (((i % c) + (i / c)) * ((i % c) + (i / c)) == i) cout << setw(n) << setfill('0') << i << endl; } return 0; }

Q579: ClockHands

//Q579 ClockHands //Accepted 2008-04-14 15:09:19 #include <iostream> #include <iomanip> #include <cmath> using namespace std; int main(void) { while(1) { float hr = 0, min = 0, degree; char time[10], hrn[3] = {'\0', '\0', '\0'}; cin >> time; if (strcmp(time, "0:00") == 0) break; if (strlen(time) == 4) { strncpy(hrn, time, 1); hr = atoi(hrn); min = atoi(time + 2); } else { strncpy(hrn, time, 2); hr = atoi(hrn); min = atoi(time + 3); } degree = abs((hr + (min / 60))* 30 - min * 6); if (degree > 180) degree -= 360; cout << setprecision(3) << fixed << abs(degree) << endl; } }

Q458: The Decoder

//Q458 The Decoder //Accepted 2008-04-14 13:45:08 #include <iostream> using namespace std; int main(void) { while(1) { char encrypted[100]; cin.getline(encrypted, 100); if (strlen(encrypted) == 0) break; for(int i = 0; i < strlen(encrypted); i++) encrypted[i] -= 7; cout << encrypted << endl; } }

Q160: Factors and Factorials

//Q160 Factors and Factorials //Accepted 2008-04-13 21:09 #include <iostream> #include <iomanip> #include <cmath> using namespace std; int ispf(int); int n; int main(void) { int i, j, nt, pn, //total primefacter number pf[100], //primefacters pfn[100]; //each primefacters contains while(1) { pn = 0; cin >> n; if (n == 0) break; nt = n; for(i = 2; i <= nt; i++) { if (ispf(i) == 1) pf[++pn] = i; } for(i = 1; i <= pn; i++) { nt = n; pfn[i] = 0; while(nt > 0) { pfn[i] += (nt / pf[i]); nt /= pf[i]; } } cout << setw(3) << n << "! ="; for(i = 1; i <= pn; i++) ...

Q10929: You can say 11

#include <iostream> //2008/4/12 int main(void) { long int a = 0, b = 0; int i, l = 0; char s[1001]; while(1) { cin >> s; if (strcmp(s, "0") == 0) break; for(i = 0; i < strlen(s); i++) { if (i % 2 == 0) a += s[i] - '0'; else b += s[i] - '0'; } if ((b - a) % 11 == 0) printf("%s is a multiple of 11.\n", s); else printf("%s is not a multiple of 11.\n", s); a = 0; b = 0; } }

Q100: The 3n + 1 problem

不知道是不是太久沒寫C++了 寫起來卡卡的 於是我就先從ACM的題目下手 翻了翻我Gmail信箱,竟然翻到了一封"化石"! 那是我高一開是玩ACM寫的 最簡單的: Q100: The 3n + 1 problem 看了突然覺得那時候用ANSI C寫的有夠爛~所以我就重新寫了一次 這次是用C++ po給大家對照一下吧 這是高一寫的(with C) #include <stdio.h> //2006/8/10 int main(void) { long int i, t, bg, a, b, c; char ch; bg = 0; while (scanf("%d%d", &a, &b) == 2) { if (a > b) { t = a; a = b; b = t; } for(i = a; i <= b; i++) { t = i; c = 1; while (t != 1) { if (t % 2 == 1) t = t * 3 +1; else t /= 2; c++; } if (c > bg) bg = c; } printf("%d %d %d", a, b, bg); } } 這是我昨天寫的(with C++) //Q100 The 3n + 1 problem //Accepted 2008-04-12 #include <iostream> using namespace std; int circlelength(unsigned long int); int main(void) { int i, j, k, temp, bg = 0;...