1.求倒序数(66.6%)
#include <bits/stdc++.h> using namespace std; int main () { string str; cin >> str; bool flag = false; reverse(str.begin(), str.end()); for (const auto& c : str) if (c < '0' || c > '9') flag = true; long long ret = stoll(str); if (ret > INT_MAX || ret < INT_MIN && flag) cout << "error" << endl; else cout << ret << endl; return 0; }
2.求折叠链表(60%)
#include <bits/stdc++.h> using namespace std; int main() { string str; getline(cin, str); vector<int> nums; string temp; for (const auto& c : str) { if (c <= '9' && c >= '0') { temp += c; } else { if (!temp.empty()) nums.push_back(stoi(temp)); temp.clear(); } } if (!temp.empty()) { nums.push_back(stoi(temp)); temp.clear(); } vector<int> out; string output = "["; int l = 0, r = nums.size() - 1; while (l <= r) { out.push_back(nums[l]); if (l != r) out.push_back(nums[r]); l++; r--; } for (const auto& c : out) { output += to_string(c) + ','; } output.pop_back(); output += ']'; cout << output << endl; return 0; }不太清楚异常测试用例,随便做一下了。精力有限,先写论文了。
全部评论
(2) 回帖