贝壳8.13笔试,1,2题
第一题:
如果暴力解的话,会超时。
这里注意long long,不然long long 转到int会丢数据,导致结果不正确
vector<long> FarmerNN(int n, long long m) { // write code here vector<long> tian(n, 0); long long yushu = m % (2 * (n - 1)); long long sum = (m- yushu )/ (2 * (n - 1)); tian[0] = sum; tian[n-1] = sum; sum = 2 * sum; for (int i = 1; i < tian.size()-1; i++) { tian[i] = sum; } long long sa = 0; int p = 0; bool flag = 0;//0,向前+,1向后- while (sa < yushu) { //到头了,转向 if (p == (n - 1)) { flag = 1; } if (p == 0) { flag = 0; } if (flag == 0) { tian[p] += 1; ++p; } else { tian[p] += 1; --p; } ++sa; } return tian; }
第二题:
这个就超级简单
string NS_String(string s, int k) { // write code here string result = s; for (int t = 0; t < k; t++) { int size = s.size(); int min = 500; int p = 0; for (int i = 0; i < size; i++) { if (s[i]-'a'<=min) { min = s[i] - 'a'; p = i; } } //把找出来的最小的字符都删掉 char temp = s[p]; //这块注意for的结束条件i < s.size();,还有--i; for (int i = 0; i < s.size(); i++) { if (temp == s[i]) { s.erase(i, 1); --i; } } cout << s << endl; } return s; }#贝壳笔试##贝壳找房##笔经#