(1)魔法树高度
#include<iostream> #include<string> #include<algorithm> #include<vector> using namespace std; int main() { long n; cin >> n; vector<int>array; if (n<1 || n>1000000) return 0; for (int i = 0; i<n; i++) { int temp; cin >> temp; array.push_back(temp); } int cnt = 0; bool flag = false; while (flag == false) // 存在不相等的元素 { sort(array.rbegin(), array.rend()); // 降序 cnt++; for (int j = 1; j<array.size(); j++) array[j]++; int num = count(array.begin(), array.end(), array[0]); if (num == array.size()) flag = true; } cout << cnt << endl; return 0; }(2)字符串替换
#include<iostream> #include<string> #include<algorithm> #include<vector> using namespace std; int main() { string s; cin >> s; long n; cin >> n; vector<int>data; vector<vector<int>>bianhuan;// for (int i = 0; i<n; i++) // 获取变换的字符 { for (int j = 0; j<2; j++) { int temp; cin >> temp; data.push_back(temp); } bianhuan.push_back(data); data.clear(); } for (int i = 0; i<bianhuan.size(); i++) // 替换 { int j, k; j = 0; k = s.size() - 1; while (j<=k) { if (s[j] - '0' == bianhuan[i][0]) // 从前搜索 s[j] = bianhuan[i][1] + '0'; j++; if (s[k] - '0' == bianhuan[i][0]) // 从后搜索 s[k] = bianhuan[i][1] + '0'; k--; } } cout << s << endl; return 0; }
听说点赞的人都可以拿到offer噢,大家七夕快乐🤣🤣🤣
全部评论
(1) 回帖