<script charset="Shift_JIS" src="http://chabudai.sakura.ne.jp/blogparts/honehoneclock/honehone_clock_tr.js"></script>
忘记题目了,吃豆子游戏#include<math.h> #include<stdio.h> #include<string.h> #include<assert.h> #include<limits.h> #include<stdbool.h> #include<stdlib.h> #include<iostream> using namespace std; int data[150][150] = { 0 }; int main() { int n, m; while (cin >> n >> m) { memset(data, 0, 150 * 150); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { char c; cin >> c; if ('W' == c) data[i][j] = 1; } } int ii; int flag = true; for (ii = n-1; ii>=0 && flag; ii--) { for (int j = 0; j < m && flag; j++) { if (data[ii][j]> 0 && flag ) { flag = false; } } } ii++; int count = 0; int x = 0; int y = -1; int r = -1; for (int i = 0; i <= ii; i++) { int begin = 0; int end = m - 1; while (begin <n && data[i][begin] == 0 ) begin++; while (end >= 0 && data[i][end] == 0) end--; if (begin == n && end == -1) { y++; r = -r; count++; } else if (begin <= end) { if (r == -1) { if (x <= begin) { y++; r = -r; count += end - x + 1; x = end; } else { y++; r = -r; count += end - begin + x - begin + 1; x = end; } } else { if (x >= end) { y++; r = -r; count += x - begin+ 1; x = begin; } else { y++; r = -r; count += end - begin + end - x + 1; x = end; } } } } cout << count - 1; } }
卡牌组合技
#include<math.h> #include<stdio.h> #include<string.h> #include<assert.h> #include<limits.h> #include<stdbool.h> #include<stdlib.h> #include<iostream> #include<algorithm> using namespace std; int data[20] = { 0 }; int da[20][10000007] = { 0 }; int sum = 0; int main() { int n, m; while (cin >> n >> m) { for (int i = 0; i < n; i++) { cin >> data[i]; sum += data[i]; } std::sort(data, data + n); int big = -1000000007; memset(da, 0, sum + 1); for (int i = 0; i < n; i++) { da[i][0] = 1; } da[0][data[0]] = data[0]; for (int i = 1; i < n; i++) { for (int j = 0; j < data[i]; j++) { da[i][j] = da[i - 1][j]; } for (int j = data[i] ; j <= m; j++) { da[i][j] = std::max(da[i - 1][j], da[i - 1][j - data[i]] * data[i]); } } cout << da[n - 1][m] << endl;; } }
公平的匹配赛
#include<math.h> #include<stdio.h> #include<string.h> #include<assert.h> #include<limits.h> #include<stdbool.h> #include<stdlib.h> #include<iostream> #include<algorithm> using namespace std; int data[100]; int da[100][10000] = { 0}; int sum = 0; bool equit(int* data, int n) { if (n % 2) return false; int sum = 0; for (int i = 0; i < n; i++) { sum += data[i]; } da[0][data[0]] = 1; for (int i = 1; i < n; i++) { for (int j = 0; j < data[i]; j++) { da[i][j] = da[i - 1][j]; } for (int j = data[i]; j <= sum / 2; j++) { da[i][j] = std::max(da[i-1][j - data[i]] + 1, da[i-1][j]); } } if (da[n - 1][sum / 2] >= n / 2) return true; else return false; } int main() { int n, m; cin >> n >> m; { memset(data, 0, 100); memset(da, 0, 10000 * 100); for (int j = 0; j < n; j++) { { int sum = 0; bool result = false; for (int i = 0; i < m; i++) { cin >> data[i]; } result = equit(data, m); if (result) cout << "TRUE" << endl; else cout << "FALSE" << endl; } } } }
这次犯了错误,for循环里面for循环break只对里层for循环有效,切记切记。后面两道动态规划的一时没想出来,交完卷一个小时就写出来了,也没有在线编译,例子是通过的
吃豆子 卡牌组合技 公平的匹配赛
最新推荐文章于 2024-08-22 17:27:46 发布