|
1 #include <iostream> 2 3 using namespace std; 4 5 struct Record 6 { 7 int a, b; 8 9 bool operator < (const Record & r) const 10 { 11 if (b == r.b) 12 return a < r.a; 13 return b < r.b; 14 } 15 } rec[10000]; 16 17 int recCnt; 18 19 int n, m; 20 int bisqCnt, bisq[125000 + 1]; 21 bool isBisq[125000 + 1]; 22 23 int main() 24 { 25 freopen("ariprog.in", "r", stdin); 26 freopen("ariprog.out", "w", stdout); 27 28 cin >> n >> m; 29 30 for (int p = 0; p <= m; p++) 31 for (int q = 0; q <= m; q++) 32 isBisq[p * p + q * q] = true; 33 34 for (int i = 0; i <= 2 * m * m; i++) 35 if (isBisq[i]) 36 bisq[bisqCnt++] = i; 37 38 for (int i = 0; i <= bisqCnt - n; i++) 39 { 40 for (int j = i + 1; j <= bisqCnt - n + 1; j++) 41 { 42 int a = bisq[i]; 43 int b = bisq[j] - bisq[i]; 44 45 int k; 46 for (k = 2; k < n; k++) 47 { 48 if (a + b * k > 2 * m * m) 49 break; 50 if (isBisq[a + b * k] == false) 51 break; 52 } 53 if (k == n) 54 { 55 rec[recCnt].a = a; 56 rec[recCnt].b = b; 57 recCnt++; 58 } 59 } 60 } 61 62 sort(rec, rec + recCnt); 63 64 if (recCnt == 0) 65 cout << "NONE" << endl; 66 67 for (int i = 0; i < recCnt; i++) 68 cout << rec[i].a << ' ' << rec[i].b << endl; 69 70 return 0; 71 } 72
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 freopen("clocks.in", "r", stdin); 8 freopen("clocks.out", "w", stdout); 9 10 enum { A, B, C, D, E, F, G, H, I }; 11 12 int control[9][6] = { 13 {4, A, B, D, E}, 14 {3, A, B, C}, 15 {4, B, C, E, F}, 16 {3, A, D, G}, 17 {5, B, D, E, F, H}, 18 {3, C, F, I}, 19 {4, D, E, G, H}, 20 {3, G, H, I}, 21 {4, E, F, H, I} 22 }; 23 24 int oriClocks[9]; 25 int newClocks[9]; 26 27 for(int i = 0; i < 9; i++) 28 { 29 cin >> oriClocks[i]; 30 switch (oriClocks[i]) 31 { 32 case 3 : oriClocks[i] = 1; break; 33 case 6 : oriClocks[i] = 2; break; 34 case 9 : oriClocks[i] = 3; break; 35 case 12 : oriClocks[i] = 0; break; 36 } 37 } 38 39 int x[9], bestx[9], bestLen = 65535; 40 for (x[0] = 0; x[0] < 4; x[0]++) 41 for (x[1] = 0; x[1] < 4; x[1]++) 42 for (x[2] = 0; x[2] < 4; x[2]++) 43 for (x[3] = 0; x[3] < 4; x[3]++) 44 for (x[4] = 0; x[4] < 4; x[4]++) 45 for (x[5] = 0; x[5] < 4; x[5]++) 46 for (x[6] = 0; x[6] < 4; x[6]++) 47 for (x[7] = 0; x[7] < 4; x[7]++) 48 for (x[8] = 0; x[8] < 4; x[8]++) 49 { 50 for (int i = 0; i < 9; i++) 51 newClocks[i] = oriClocks[i]; 52 for (int i = 0; i < 9; i++) 53 for (int j = 1; j <= control[i][0]; j++) 54 newClocks[control[i][j]] += x[i]; 55 for (int i = 0; i < 9; i++) 56 newClocks[i] %= 4; 57 58 bool allZero = true; 59 for (int i = 0; i < 9; i++) 60 if (newClocks[i] != 0) 61 { 62 allZero = false; 63 break; 64 } 65 66 if (allZero) 67 { 68 int len = 0; 69 for (int i = 0; i < 9; i++) 70 len += x[i]; 71 if (len < bestLen) 72 { 73 bestLen = len; 74 for (int i = 0; i < 9; i++) 75 bestx[i] = x[i]; 76 } 77 } 78 } 79 80 for (int i = 0, cnt = 0; i < 9; i++) 81 for (int j = 0; j < bestx[i]; j++) 82 { 83 cnt++; 84 cout << i + 1 << (cnt == bestLen ? '\n' : ' '); 85 } 86 87 return 0; 88 } 89
1 #include <iostream> 2 3 using namespace std; 4 5 int max(int a, int b, int c) 6 { 7 return max(max(a, b), c); 8 } 9 10 int max(int a, int b, int c, int d) 11 { 12 return max(max(a, b, c), d); 13 } 14 15 struct Rectangle 16 { 17 int h; //height 18 int w; //width 19 } rect[4]; 20 21 int ans = 65535; 22 int ans_cnt = 0; 23 int ans_w[24], ans_h[24]; 24 25 void updateAns(int tot_w, int tot_h) 26 { 27 if (tot_w * tot_h <= ans) 28 { 29 if (tot_w * tot_h < ans) 30 { 31 ans = tot_w * tot_h; 32 ans_cnt = 0; 33 } 34 for (int i = 0; i < ans_cnt; i++) 35 if (ans_w[i] == min(tot_w, tot_h) && ans_h[i] == max(tot_w, tot_h)) 36 return; 37 ans_w[ans_cnt] = min(tot_w, tot_h); 38 ans_h[ans_cnt] = max(tot_w, tot_h); 39 ans_cnt++; 40 } 41 } 42 43 void calcArea(int x[], int i) 44 { 45 int h0 = rect[x[0]].h; int w0 = rect[x[0]].w; 46 int h1 = rect[x[1]].h; int w1 = rect[x[1]].w; 47 int h2 = rect[x[2]].h; int w2 = rect[x[2]].w; 48 int h3 = rect[x[3]].h; int w3 = rect[x[3]].w; 49 if ((i & 1) == 1) swap(h0, w0); 50 if ((i & 2) == 2) swap(h1, w1); 51 if ((i & 4) == 4) swap(h2, w2); 52 if ((i & 8) == 8) swap(h3, w3); 53 54 int tot_w, tot_h; 55 56 //1 57 tot_h = max(h0, h1, h2, h3); 58 tot_w = w0 + w1 + w2 + w3; 59 updateAns(tot_w, tot_h); 60 //2 61 tot_h = max(h0, h1, h2) + h3; 62 tot_w = max(w0 + w1 + w2, w3); 63 updateAns(tot_w, tot_h); 64 //3 65 tot_h = max(max(h0, h1) + h3, h2); 66 tot_w = max(w0 + w1, w3) + w2; 67 updateAns(tot_w, tot_h); 68 //4 69 tot_h = max(h0 + h1, h2, h3); 70 tot_w = max(w0, w1) + w2 + w3; 71 updateAns(tot_w, tot_h); 72 //5 73 tot_h = max(h0 + h1, h2 + h3); 74 tot_w = max(w0 + w3, w1 + w2); 75 if (h0 + h2 > tot_h && w0 + w2 > tot_w) return; 76 if (h1 + h3 > tot_h && w1 + w3 > tot_w) return; 77 updateAns(tot_w, tot_h); 78 } 79 80 void getPermutation(int x[], int p) 81 { 82 if (p >= 4) 83 { 84 for (int i = 0; i <= 15; i++) 85 calcArea(x, i); 86 return; 87 } 88 for (int i = p; i < 4; i++) 89 { 90 swap(x[i], x[p]); 91 getPermutation(x, p + 1); 92 swap(x[i], x[p]); 93 } 94 } 95 96 int main() 97 { 98 freopen("packrec.in", "r", stdin); 99 freopen("packrec.out", "w", stdout); 100 101 for (int i = 0; i < 4; i++) 102 cin >> rect[i].w >> rect[i].h; 103 104 int x[4] = { 0, 1, 2, 3 }; //the permutation of 4 rectangles 105 106 getPermutation(x, 0); 107 108 for (int i = 0; i < ans_cnt - 1; i++) 109 for (int j = i + 1; j < ans_cnt; j++) 110 if (ans_w[i] > ans_w[j]) 111 { 112 swap(ans_w[i], ans_w[j]); 113 swap(ans_h[i], ans_h[j]); 114 } 115 116 cout << ans << endl; 117 for (int i = 0; i < ans_cnt; i++) 118 cout << ans_w[i] << ' ' << ans_h[i] << endl; 119 120 return 0; 121 } 122
1 #include <iostream> 2 3 using namespace std; 4 5 bool find(int x[], int n, int m) 6 { 7 for (int i = 0; i < n; i++) 8 if (x[i] == m) 9 return true; 10 return false; 11 } 12 13 int main() 14 { 15 freopen("crypt1.in", "r", stdin); 16 freopen("crypt1.out", "w", stdout); 17 18 int n, x[10]; 19 20 cin >> n; 21 for (int i = 0; i < n; i++) 22 cin >> x[i]; 23 24 /* 25 a b c 26 x p q 27 ------- 28 * * * == i 29 * * * == j 30 ------- 31 * * * * == k 32 */ 33 int ans = 0; 34 for (int a = 0; a < n; a++) 35 for (int b = 0; b < n; b++) 36 for (int c = 0; c < n; c++) 37 for (int p = 0; p < n; p++) 38 for (int q = 0; q < n; q++) 39 { 40 int i = (x[a] * 100 + x[b] * 10 + x[c]) * x[q]; 41 int j = (x[a] * 100 + x[b] * 10 + x[c]) * x[p] * 10; 42 int k = i + j; 43 44 if (i >= 1000 || j >= 10000 || k >= 10000) 45 continue; 46 else 47 { 48 //check i 49 if (find(x, n, i / 1 % 10) == false || 50 find(x, n, i / 10 % 10) == false || 51 find(x, n, i / 100 % 10) == false) 52 continue; 53 //check j 54 if (find(x, n, j / 10 % 10) == false || 55 find(x, n, j / 100 % 10) == false || 56 find(x, n, j / 1000 % 10) == false) 57 continue; 58 //check k 59 if (find(x, n, k / 1 % 10) == false || 60 find(x, n, k / 10 % 10) == false || 61 find(x, n, k / 100 % 10) == false || 62 find(x, n, k / 1000 % 10) == false) 63 continue; 64 ans += 1; 65 } 66 } 67 68 cout << ans << endl; 69 70 return 0; 71 } 72
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 freopen("calfflac.in", "r", stdin); 8 freopen("calfflac.out", "w", stdout); 9 10 string oristr; 11 string newstr; 12 int pos[20002]; 13 14 while (true) 15 { 16 char c = getchar(); 17 if (c == EOF) 18 break; 19 else 20 oristr += c; 21 } 22 23 for (int i = 0, n = 0; i < oristr.size(); i++) 24 if (isalpha(oristr[i])) 25 newstr += tolower(oristr[i]), pos[n++] = i; 26 27 int ans_len = 0; 28 int ans_pos = 0; 29 for (int k = 0; k < newstr.size(); k++) 30 { 31 int i = k, j = k; 32 33 while (i - 1 >= 0 && j + 1 < newstr.size() && newstr[i - 1] == newstr[j + 1]) 34 i -= 1, j += 1; 35 if (j - i + 1 > ans_len) 36 { 37 ans_len = j - i + 1; 38 ans_pos = i; 39 } 40 41 if (k + 1 < newstr.size() && newstr[k] == newstr[k + 1]) 42 { 43 i = k, j = k + 1; 44 while (i - 1 >= 0 && j + 1 < newstr.size() && newstr[i - 1] == newstr[j + 1]) 45 i -= 1, j += 1; 46 if (j - i + 1 > ans_len) 47 { 48 ans_len = j - i + 1; 49 ans_pos = i; 50 } 51 } 52 } 53 54 cout << ans_len << endl; 55 for (int i = pos[ans_pos], n = 0; n < ans_len; i++) 56 { 57 cout << oristr[i]; 58 if (isalpha(oristr[i])) 59 n++; 60 } 61 cout << endl; 62 63 return 0; 64 } 65
code 1
1 #include <iostream> 2 3 using namespace std; 4 5 class Interval 6 { 7 public: 8 int length, start; 9 10 bool operator < (const Interval & i) const 11 { 12 return length < i.length; 13 } 14 } interval[200]; 15 16 int main() 17 { 18 freopen("barn1.in", "r", stdin); 19 freopen("barn1.out", "w", stdout); 20 21 int n; //the maximum number of boards that can be purchased 22 int m; //the number of cows in the stalls 23 int x[200]; //the number of each occupied stall 24 25 cin >> n >> m >> m; 26 for (int i = 0; i < m; i++) 27 cin >> x[i]; 28 29 sort(x, x + m); 30 31 for (int i = 0; i < m - 1; i++) 32 { 33 interval[i].start = x[i]; 34 interval[i].length = x[i + 1] - x[i] - 1; 35 } 36 37 sort(interval, interval + m - 1); 38 39 int ans = x[m - 1] - x[0] + 1; 40 41 n -= 1; 42 for (int i = m - 2; i >= 0 && n; i--) 43 ans -= interval[i].length, n--; 44 45 cout << ans << endl; 46 47 return 0; 48 } 49
code 2
1 #include <iostream> 2 3 using namespace std; 4 5 int main() 6 { 7 freopen("barn1.in", "r", stdin); 8 freopen("barn1.out", "w", stdout); 9 10 int n, m, x[500], f[500][500]; 11 12 cin >> n >> m >> m; 13 for (int i = 1; i <= m; i++) 14 cin >> x[i]; 15 16 sort(x + 1, x + 1 + m); 17 18 f[1][1] = 1; 19 for (int i = 2; i <= m; i++) 20 f[1][i] = f[1][i - 1] + x[i] - x[i - 1]; 21 for (int i = 2; i <= min(n, m); i++) 22 for (int j = i; j <= m; j++) 23 { 24 f[i][j] = 65535; 25 for (int k = i; k <= j; k++) 26 f[i][j] <?= f[i - 1][k - 1] + (x[j] - x[k] + 1); 27 } 28 29 cout << f[min(n, m)][m] << endl; 30 31 return 0; 32 } 33
1 #include <iostream> 2 3 using namespace std; 4 5 class Milk 6 { 7 public: 8 int price; 9 int amount; 10 11 bool operator < (const Milk & m) const 12 { 13 return price < m.price; 14 } 15 } milk[5000]; 16 17 int main() 18 { 19 freopen("milk.in", "r", stdin); 20 freopen("milk.out", "w", stdout); 21 22 int n, m, ans = 0; 23 24 cin >> n >> m; 25 for (int i = 0; i < m; i++) 26 cin >> milk[i].price >> milk[i].amount; 27 28 sort(milk, milk + m); 29 30 int i = 0; 31 while (n) 32 { 33 ans += min(milk[i].amount, n) * milk[i].price; 34 n -= min(milk[i].amount, n); 35 i += 1; 36 } 37 38 cout << ans << endl; 39 40 return 0; 41 } 42
1 #include <iostream> 2 3 using namespace std; 4 5 string num2string(int n, int b) 6 { 7 string s; 8 while (n) 9 { 10 s += "0123456789"[n % b]; 11 n /= b; 12 } 13 for (unsigned i = 0; i < s.size() / 2; i++) 14 swap(s[i], s[s.size() - i - 1]); 15 return s; 16 } 17 18 bool isPalindrome(const string & s) 19 { 20 for (unsigned i = 0; i < s.size() / 2; i++) 21 if (s[i] != s[s.size() - i - 1]) 22 return false; 23 return true; 24 } 25 26 int main() 27 { 28 freopen("dualpal.in", "r", stdin); 29 freopen("dualpal.out", "w", stdout); 30 31 int n, s; 32 33 cin >> n >> s; 34 for (int i = 0, j = s + 1; i < n; j++) 35 { 36 int cnt = 0; 37 for (int b = 2; b <= 10; b++) 38 cnt += isPalindrome(num2string(j, b)); 39 if (cnt >= 2) 40 { 41 cout << j << endl; 42 i += 1; 43 } 44 } 45 46 return 0; 47 } 48
1 #include <iostream> 2 3 using namespace std; 4 5 //convert a number to string in base b 6 string num2string(int n, int b) 7 { 8 string s; 9 while (n) 10 { 11 s += "0123456789ABCDEFGHIJ"[n % b]; 12 n /= b; 13 } 14 for (unsigned i = 0; i < s.size()/ 2; i++) 15 swap(s[i], s[s.size() - i - 1]); 16 return s; 17 } 18 19 bool isPalindrome(const string & s) 20 { 21 for (unsigned i = 0; i < s.size() / 2; i++) 22 if (s[i] != s[s.size() - i - 1]) 23 return false; 24 return true; 25 } 26 27 int main() 28 { 29 freopen("palsquare.in", "r", stdin); 30 freopen("palsquare.out", "w", stdout); 31 32 int b; 33 cin >> b; 34 for (int i = 1; i <= 300; i++) 35 if (isPalindrome(num2string(i * i, b)) == true) 36 cout << num2string(i, b) << ' ' << num2string(i * i, b) << endl; 37 38 return 0; 39 } 40
1 #include <fstream> 2 3 using namespace std; 4 5 int letter2num(const char c) 6 { 7 switch (c) 8 { 9 case 'A' : case 'B' : case 'C' : return 2; 10 case 'D' : case 'E' : case 'F' : return 3; 11 case 'G' : case 'H' : case 'I' : return 4; 12 case 'J' : case 'K' : case 'L' : return 5; 13 case 'M' : case 'N' : case 'O' : return 6; 14 case 'P' : case 'R' : case 'S' : return 7; 15 case 'T' : case 'U' : case 'V' : return 8; 16 case 'W' : case 'X' : case 'Y' : return 9; 17 } 18 } 19 20 int main() 21 { 22 ifstream fin("namenum.in"); 23 ifstream fin_dict("dict.txt"); 24 ofstream fout("namenum.out"); 25 26 string num, name; 27 int cnt = 0; 28 29 fin >> num; 30 while (fin_dict >> name) 31 { 32 if (num.size() != name.size()) 33 continue; 34 35 unsigned i; 36 for (i = 0; i < name.size(); i++) 37 if (letter2num(name[i]) != num[i] - '0') 38 break; 39 if (i == name.size()) 40 { 41 cnt++; 42 fout << name << endl; 43 } 44 } 45 46 if (cnt == 0) 47 fout << "NONE" << endl; 48 49 return 0; 50 } 51
|