富途9.13笔试
1.分糖果
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int num = 0, maxval = 0; cin >> num >> maxval; vector<int> candy(num); for (int i = 0; i < num; i++) cin >> candy[i]; sort(candy.begin(), candy.end()); //int num = 3, maxval = 10; //vector<int> candy = { 2,2,5 }; int count = 0, sum = 0; for (; count < num && maxval >= sum; count++) { sum += candy[count]; } //int res = maxval == 0 ? count + 1 : count; if (count == num && maxval >= sum) cout << count; else cout << count - 1; return 0; }2.求一次项系数
#include <iostream> #include <string> #include <vector> using namespace std; int main() { string str; cin >> str; //string str = "(x-1)(x+2)"; vector<int> nums; for (int i = 0; i < str.size(); i++) { if (str[i] == '(') { int sign = str[i + 2] == '-' ? -1 : 1; nums.push_back(sign * (str[i + 3] - '0')); i += 4; /*i += 3; int begin = i; i++; while (i < str.size() && str[i] != ')') i++; nums.push_back(sign * stoi(str.substr(begin, i - begin)));*/ } } //vector<int> nums = { -1,2,3 }; vector<int> multi; for (int i = 0; i < nums.size(); i++) { int num = 1; for (int j = 0; j < nums.size(); j++) { if (j != i) num *= nums[j]; } multi.push_back(num); } if (multi.size() == 1) { cout << multi[0]; return 0; } int res = 0; for (int i = 0; i < multi.size(); i++) { res += multi[i]; } cout << res % 10007; return 0; }