从1..n中间选取任意组合,其和为m,列出所有组合的算法。
来源:互联网 发布:sql中round的用法 编辑:程序博客网 时间:2024/05/16 18:34
采用递推方法,并将每次的结果保存,并做为下一个的计算的基础。
/// <summary>
/// 从1..n中间选取任意组合,其和为m
/// </summary>
/// <param name="n"></param>
/// <param name="m"></param>
private void ListComboDigit(int n, int m)
{
//显示n*(n+1)小于2*m则无解
if (n * (n + 1) < 2 * m)
return;
int iItems = 0;
List<List<List<int>>> theRets = new List<List<List<int>>>();
for (int i = 1; i <= m; i++)
{
List<List<int>> theCurrRets = new List<List<int>>();
for (var k = 1; k <= i - 1; k++)
{
int tmp = i - k;
if (tmp > 0 && tmp <= n)
{
foreach (var item in theRets[k - 1])
{
if (tmp > item[item.Count - 1])
{
List<int> theTmp = new List<int>();
theTmp.AddRange(item);
theTmp.Add(tmp);
theCurrRets.Add(theTmp);
}
iItems++;
}
}
}
if (i <= n)
{
List<int> theSelf = new List<int>();
theSelf.Add(i);
theCurrRets.Add(theSelf);
}
iItems++;
theRets.Add(theCurrRets);
}
this.richTextBox1.Text = "";
foreach (var item in theRets[m - 1])
{
string tmpstr = "";
foreach (var it in item)
{
tmpstr += it.ToString() + " ";
}
this.richTextBox1.Text += tmpstr + "/r/n";
}
}
- 从1..n中间选取任意组合,其和为m,列出所有组合的算法。
- 输入两个整数n和m,从数列1,2,3...n中随意取几个数,使其和等于m,要求列出所有的组合
- 算法题:求从n个数组任意选取一个元素的所有组合
- 写正确函数需要注意的地方:输入两个整数n和m,从数列1,2,3,...n中随意取几个数,使其和为m,列出所有可能组合
- 输入两个整数n和m, 从数列1,2,...,n中任意选择几个数,使其和等于m, 要求编写程序输出所有的组合
- 输入两个整数n和m, 从数列1,2,...,n中任意选择几个数,使其和等于m, 要求编写程序输出所有的组合
- 输入两个整数n和m, 从数列1,2,...,n中任意选择几个数,使其和等于m, 要求编写程序输出所有的组合
- 深搜 从n个数中随机选取m个数 求出所有组合的和 模版
- 求从1,2...n中取任意个不重复的数和为m的所有组合
- 输入两个整数n 和 m, 从数列1, 2, 3, ..., n中随意取几个数, 使其和等于m, 要求将所有组合列出来
- 输入两个整数n和m,从数列1,2,3,...,n中随意取几个数,使其和等于m,将其所有可能的组合列出来。 递归求解
- 输入两个整数n和m,从数列1,2,3,...,n中随意取几个数,使其和等于m,将其所有可能的组合列出来。 回溯求解
- 编程求解,输入两个整数n和m,从数列1,2,3,……n中随意取几个数,使其和等于m。要求将所有的可能组合列出来(背包问题求解)
- 输入两个整数 n 和 m,从数列1,2,3.......n 中 随意取几个数, 使其和等于 m ,要求将其中所有的可能组合列出来.
- 输入两个整数 n 和 m,从数列1,2,3.......n 中随意取几个数,使其和等于 m ,要求将其中所有的可能组合列出来.
- 输入两个整数 n 和 m,从数列1,2,3.......n 中随意取几个数,使其和等于 m ,要求将其中所有的可能组合列出来.
- 输入两个整数n 和m,从数列1,2,3.......n 中随意取几个数,使其和等于m ,要求将其中所有的可能组合列出来.
- 输入两个整数n 和m,从数列1,2,3.......n 中随意取几个数,使其和等于m ,要求将其中所有的可能组合列出来.
- 《COM技术内幕》阅读笔记第7章
- if 和 else if 的效率问题
- 12.VC(custom)-MFC应用程序配置不正确解决方案
- 市民卡
- 市民卡
- 从1..n中间选取任意组合,其和为m,列出所有组合的算法。
- fread 和 read的区别
- ORACLE的utl_file包
- ext的grid导出为excel 方法(转http://leoni236.blog.163.com/blog/static/4814624920109892520226/)
- DOM
- PHP获取checkbox值
- 刚来
- MyEclipse SVN插件冲突导致不能使用解决办法
- 小结顺序容器的使用