想法:a,a+1,...,a+n-1共n个数相加
和为n*(2*a+n-1)/2=100
简单推论:a<50或a=100;n>=2或n==1
因此代码可以写为:
#include<iostream>
using namespace std;
int main()
{
for(int i = 2; i < 50; ++i)
{
int sum = i;
for(int j = i+1; j < 50; ++j)
{
sum += j;
if(sum == 100)
{
for (int k = i; k <= j; ++k)
{
cout << k << " + ";
}
cout << "= 100" << endl;
break;
}
if(sum>100)
break;
}
}
cout << "100 = 100 "<< endl;
}