不知道怎么回事就是不能全A,心态都崩了
第一题:
#include <iostream> #include <unordered_map> using namespace std; int main() { string str; cin >> str; unordered_map<char, int> hash; int res = 0; int i = 0; int j = 0; int n = str.size(); while (i < n && j < n) { if (hash.find(str[j]) == hash.end()) { hash[str[j]] = j; ++j; res = max(res, j - i); } else { hash.erase(str[i]); ++i; } } cout << res; return 0; }第二题:
#include <iostream> #include <vector> #include <unordered_map> using namespace std; int main() { vector<int> nums; int n; while (cin >> n) { if (cin.get() == '\n') break; nums.push_back(n); } unordered_map<int, int> m; int i; n = nums.size(); int ans = nums[0]; m[ans]++; for (i = 1; i < n; i++) { m[nums[i]]++; if (m[nums[i]] > m[ans]) ans = nums[i]; } cout << ans << endl; return 0; }第三题:
#include <iostream> #include <vector> #include <algorithm> #include <set> using namespace std; int main() { vector<int> nums; int tmp; while (cin >> tmp) { if (cin.get() == '\n') break; nums.push_back(tmp); } sort(nums.begin(), nums.end()); set<vector<int> > res; if (nums.size() < 3 || nums[0] > 0) return 0; for (int i = 0; i < nums.size() - 2; ++i) { int sum = 0 - nums[i]; int l = i + 1, r = nums.size() - 1; while (l < r) { if (nums[l] + nums[r] == sum) { res.insert({ nums[i], nums[l], nums[r] }); ++l; --r; } else if (nums[l] + nums[r] < sum) { ++l; } else { --r; } } } for (auto i : res) { for (auto j : i) { cout << j << " "; } cout << endl; } return 0; }
全部评论
(0) 回帖