题目:
2010年中兴面试题
编程求解:
输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数,
使其和等于 m ,要求将其中所有的可能组合列出来.
答案:
//20130128
#include <iostream>
using namespace std;
int main()
{
int n = 100;
int m = 110;
int first = 1;
int last = n;
for (int i = 1; i <= n; ++i)
{
if (first + last > m)
{
last -= 1;
}
else
{
if (first + last < m)
{
first += 1;
}
else
{
break;
}
}
}
while (last != first)
{
cout<<first<<" "<<last<<endl;
first += 1;
last -= 1;
}
return 0;
}