解法一:数学逻辑
#include <iostream>
#include <vector>
using namespace std;
int main(){
double t;
while (cin>>t){
vector<string> res;
for (int i = 1; i <= t; ++i) {
double x = t/i - (double(i-1)/2);
string tem = to_string(x);
if (stoi(tem.substr(0,tem.find('.')))==0) break;
if (stoi(tem.substr(tem.find('.')+1)) != 0) continue;
string out;
for (int j = 0; j < i; ++j) {
out += to_string(int(x+j)) + '+';
}
res.push_back(out.substr(0,out.size()-1));
}
for (auto r:res) {
cout<<t<<"="<<r<<endl;
}
cout<<"Result:"<<res.size()<<endl;
}
return 0;
}
解法二:动态规划
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
double t;
while (cin>>t){
vector<string> res;
// dp[i][j]表示第i个元素到第j个元素的总和
vector<vector<int>> dp(t+1,vector<int>(t+1,0));
for (int i = 1; i <= t; ++i) {
for (int j = i; j <= t; ++j) {
if (i==j) dp[i][j] = i;
else dp[i][j] = dp[i][j-1] + j;
if (dp[i][j]==t){
string out;
for (int k = i; k <= j; ++k) {
out += to_string(k) + '+';
}
res.push_back(out.substr(0,out.size()-1));
}
}
}
sort(res.begin(), res.end());
for (auto it = res.rbegin();it != res.rend();it++) {
cout<<t<<"="<<*it<<endl;
}
cout<<"Result:"<<res.size()<<endl;
}
return 0;
}