1+2 = 3
4+5 = 9
2+3+4 = 9
打印1000内所有这样的数
#include<iostream>
using namespace std;
int AddNum(int startnum,int endnum)
{
int sum = 0;
for (int i = startnum;i <= endnum;i++)
{
sum += i;
}
return sum;
}
void FindNum(int num)
{
if (num == 1)
{
cout<<num<<endl;
}
else
{
int startnum = 1,endnum = 2;
while (AddNum(startnum,endnum) != num)
{
AddNum(startnum,endnum)>num?(startnum++):(endnum++);
}
if (startnum == endnum)
{
cout<<"Can not find num"<<endl;
}
else
{
for (int i = startnum;i <= endnum;i++)
{
cout<<i<<" ";
}
cout<<endl;
}
}
}
int main ()
{
for (int i = 0;i < 1000;i++)
{
FindNum(i);
}
system("pause");
return 0;
}