贪心算法最优分解问题代码
时间: 2023-11-19 18:55:27 浏览: 92
```C++
#include<iostream>
#include<vector>
using namespace std;
int main() {
int n, re = 0, sum, count = 2;
vector<int> arr(100);
arr[0] = 2;
cin >> n;
re = n - 2;
int i = 1;
while (i) {
count = count + arr[i - 1] + 1;
if (count <= n) {
arr[i] = arr[i - 1] + 1;
re = re - arr[i];
}
else break;
i++;
}
int num = i;
for (int j = re; j > 0; j--) {
arr[i - 1] = arr[i - 1] + 1; i--;
re--;
if (i == 0 && re != 0) {
j = re + 1;
i = num;
}
}
sum = arr[0];
for (int k = 1; k < num; k++) {
sum = sum * arr[k];
}
cout << sum << endl;
return 0;
}
```
阅读全文