日期:2014-05-19  浏览次数:21021 次

输入两个整数n和m,从数列1,2,3.......n 中 随意取几个数,使其和等于 m ,要求将其中所有的可能组合列出来.编程求解。
大家有什么方法.随便用什么语言.

------解决方案--------------------
for(i=0,i++,i <=INT(m/3)-1)
for(j> i,j++,j <=INT(m/3))
{
if(m-i-j> j)
Console.Writeline(i,j,m-i-j);
}
------解决方案--------------------
楼上不负责任
------解决方案--------------------
written in C#

using System;
using System.Collections.Generic;
using System.Text;

namespace nm
{
class Program
{
static bool NM(int n, int m, string r)
{
if (n > m)
n = m;
for(int i = n;i > 0;i--)
{
int _n = i - 1;
int _m = m - i;
if (_m == 0)
{
Console.WriteLine( "{0} {1} ", i, r);
continue;
}
if ((1 + _n) * _n > _m * 2)
{
string _r = Convert.ToString(i) + " " + r;
NM(_n, _m, _r);
}
else
break;
}
return true;
}

static void Main(string[] args)
{
int n;
int m;
Console.Write( "Please input n: ");
n = Convert.ToInt32(Console.ReadLine());
Console.Write( "Please input m: ");
m = Convert.ToInt32(Console.ReadLine());
string aa = " ";
NM(n, m, aa);
}
}
}

------解决方案--------------------
参考:
http://www.cnblogs.com/KissKnife/archive/2006/09/04/494688.html