题比较简单,直接贴代码
1. 格式化字符串(string遍历)
int main() { string s; cin >> s; if (s.size() == 0) { cout << s << endl; return 0; } s += 'n'; int index = 1, last = 0; vector<string> ans; while (s.find('n', index) != s.npos) { int cur = s.find('n', index); string t = s.substr(last, cur-last); t[0] -= 32; ans.push_back(t); if (cur == s.size()-1) break; last = cur; index = cur+1; } for (int i = 0; i < ans.size(); i++) cout << ans[i] << endl; return 0; }
2. 背包问题
实测:不进行状态空间压缩只能通过27%
int main() { int m, n; cin >> m >> n; vector<vector<int>> nums(m, vector<int>(n, 0)); vector<int> f(n+1, 0); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { int t; cin >> t; nums[i][j] = t; } } for (int i = 1; i <= m; i++) { for (int v = n; v >= 1; v--) { for (int k = v; k >= 1; k--) { f[v] = max(f[v], f[v-k]+nums[i-1][k-1]); } } } cout << f[n] << endl; return 0; }
全部评论
(2) 回帖