1.输出有毒...
#include <iostream> #include <vector> #include <numeric> #include <limits> #include <bits/stdc++.h> using namespace std; /*请完成下面这个函数,实现题目要求的功能 当然,你也可以不按照下面这个模板来作答,完全按照自己的想法来 ^-^ ******************************开始写代码******************************/ vector<int> divingBoard(int a, int b, int k) { int p = a; int q = b; a = min(p, q); b = max(p, q); if(k == 0) return {}; if(a == b) return {a*k}; vector<int> ans(k+1, 0); int t = a * k; ans[0] = t; for(int i = 1 ; i <= k ; i++){ t -= a; t += b; ans[i] = t; } //ans.erase(unique(ans.begin(), ans.end()), ans.end()); return ans; } /******************************结束写代码******************************/ int main() { vector < int > res; int a,b,k; cin >> a >> b >> k; res = divingBoard(a, b, k); if(res.size() == 0){ cout << "[]" << endl; return 0; } cout << '['; for(int res_i=0; res_i < res.size() - 1; res_i++) { cout << res[res_i] << ','; } cout << res[res.size()-1] << ']' << endl; return 0; }
2.处理输入建树,还要判断不合法
#include <bits/stdc++.h> using namespace std; struct TreeNode { int val; vector<TreeNode *> child; TreeNode(int x) : val(x){} }; map<string, TreeNode*> mp; bool flag; int f(TreeNode* root){ if(root == NULL) return 0; int sum = 0; for(int i = 0 ; i < root->child.size() ; i++){ sum = max(sum, f(root->child[i])); } sum += root->val; if(root->val == -1){ flag = false; } return sum; } int main() { string s; cin >> s; s += '|'; flag = true; for(int i = 0 ; i < s.length() ; i++){ int k = 0; while(s[i + k] != '`'){ k++; } string name = s.substr(i, k); i += k+1; k = 0; int sum = 0; while(s[i+k] != '`'){ sum = sum * 10 + s[i+k] - '0'; k++; } i += k; TreeNode* t; if(mp.find(name) == mp.end()){ t = new TreeNode(sum); mp[name] = t; } else{ t = mp[name]; } t->val = sum; //cout << name << " " << sum << endl; while(s[i] != '|'){ i++; k = 0; while(s[i+k] != ',' && s[i+k] != '|'){ k++; } string name = s.substr(i, k); if(name == "END"){ i += k; if(t->child.size() != 0){ cout << -1 <<endl; return 0; } break; } TreeNode* p; if(mp.find(name) == mp.end()){ p = new TreeNode(-1); mp[name] = p; } else p = mp[name]; t->child.push_back(p); i += k; } //cout << t->val << " " << t->child.size() <<endl; } int ans = f(mp["HEAD"]); if(flag == false) ans = -1; cout << ans << endl; return 0; } /* HEAD`0`A,B,C|A`20`END|B`100`END|C`50`D,E|D`80`F|E`150`END|F`30`END */
全部评论
(13) 回帖