题目描述 小王手里有点闲钱,想着作点卖水果的小买卖,给出两个数组m、n,用m[i]表示第i个水果的成本价,n[i]表示第i个水果能卖出的价钱,假如如今有本钱k元,试问最后最多能赚多少钱?ios
说明: 1.每种水果只能买一次,只能卖一次; 2.数组m,n大小不超过50; 3.数组元素为正整数,不超过1000。数组
输入描述 1.数组m, n; 2.本钱k 备注: 1.首行输入逗号分隔的数组m的元素值 2.第二行输入逗号分隔数字n的元素值 3.第三行输入本钱spa
输出描述 最多能赚多少钱。.net
示例1 输入 4,2,6,4 5,3,8,7 15 输出 22code
说明 样例计算过程: 先买前3种水果,所有卖出,再买第4种水果,再卖出,最后本金变为22。 (感受这个说明解释不正确……不是正确的解题思路……) <br><br>blog
1.思考排序
2.知识点ci
3.实现get
<br> ``` #include <iostream> #include <vector> #include <string> #include <algorithm> #include <numeric> using namespace std;input
int Str2Num(string s){ int len = s.size(); int n = 0; for (int i = 0; i < len; i++){ n = n * 10 + s[i] - '0'; } return n; }
void Apart(string str, vector<int>& num){ int pos, n, len; string s; while (!str.empty()){ len = str.size(); pos = str.find(','); if (pos > 0){ s = str.substr(0, pos); n = Str2Num(s); num.push_back(n); str = str.substr(pos + 1, len-pos-1); } else{ n = Str2Num(str); num.push_back(n); break; } } }
bool comp(pair<int, double>& a, pair<int, double>& b){ return a.second > b.second; }
int main(){ string inputm, inputn;
while (getline(cin, inputm)){ //Input vector<int> m, n; int lenin = inputm.size(); getline(cin, inputn); int k; cin >> k; Apart(inputm, m); Apart(inputn, n); //Calculate average profit and sort it int len = m.size(); vector<pair<int, double>> avr; for (int i = 0; i < len; i++){ avr.push_back(make_pair(i, (n[i] - m[i]) / (1.0*m[i]))); } sort(avr.begin(), avr.end(), comp); //Buy and sell vector<int> visit(len, 0), win; int i, idx, klast=-1; bool brk = false; while (!avr.empty()){ i = 0; while (i < len){ idx = avr[i].first; if (visit[i]==0 && m[i] < k){ k -= m[i]; win.push_back(n[i]); visit[i] = 1; } i++; } k += accumulate(win.begin(), win.end(), 0); win.clear(); if (k == klast) break; klast = k; } cout << k << endl; } return 0;
}