在10个数中选6个数,显示所有组合

BenZhuBuHuiFei 2013-03-16 05:19:05
比如"1,2,3....10" 1到10这10个数,显示他的所有可能组合情况(排列顺序无所谓)
...全文
13521 115 打赏 收藏 转发到动态 举报
写回复
用AI写文章
115 条回复
切换为时间正序
请发表友善的回复…
发表回复
lm_whales 2014-05-29
  • 打赏
  • 举报
回复
引用 141 楼 zhao4zhong1 的回复:
我的意思是任何递归算法都可以通过自己实现的栈转为非递归解法。
+++ 明白 我主要是觉得他的这个非递归,比递归还简短 你推荐的那个方法还是不错的,而且有可能可以用代码生成技术,直接转化
赵4老师 2014-05-27
  • 打赏
  • 举报
回复
引用 137 楼 lm_whales 的回复:
[quote=引用 6 楼 baipv008 的回复:]
void main()
{
	int stack[10],top=0;
	int curNum=1;
	int count=0;
	while(1)
	{
		stack[top++]=curNum;
		if(top==6)
		{
			for(int i=0;i<6;i++)
				printf("%d,",stack[i]);
			printf("\n");
			count++;
			curNum=stack[--top];
		}
		curNum++;
		while(curNum>10)
			curNum=stack[--top]+1;
		if(top<0) break;
	}
	printf("一共%d种组合",count);
	getchar();
}
这个好,非递归解法,非常巧妙[/quote] 巧妙个×http://www.codeproject.com/Articles/418776/How-to-replace-recursive-functions-using-stack-and
赵4老师 2014-05-27
  • 打赏
  • 举报
回复
我的意思是任何递归算法都可以通过自己实现的栈转为非递归解法。
lm_whales 2014-05-27
  • 打赏
  • 举报
回复
引用 138 楼 zhao4zhong1 的回复:
[quote=引用 137 楼 lm_whales 的回复:] 巧妙个×http://www.codeproject.com/Articles/418776/How-to-replace-recursive-functions-using-stack-and
文章是好,方法也不错。 不过即使仅仅能够按部就班, 设计出来 也需要和费心思的。 何况,代码这么简短。
lm_whales 2014-05-27
  • 打赏
  • 举报
回复
引用 138 楼 zhao4zhong1 的回复:
巧妙个×http://www.codeproject.com/Articles/418776/How-to-replace-recursive-functions-using-stack-and
递归解法虽然好,不过不值得推荐
lm_whales 2014-05-26
  • 打赏
  • 举报
回复
引用 6 楼 baipv008 的回复:
void main()
{
	int stack[10],top=0;
	int curNum=1;
	int count=0;
	while(1)
	{
		stack[top++]=curNum;
		if(top==6)
		{
			for(int i=0;i<6;i++)
				printf("%d,",stack[i]);
			printf("\n");
			count++;
			curNum=stack[--top];
		}
		curNum++;
		while(curNum>10)
			curNum=stack[--top]+1;
		if(top<0) break;
	}
	printf("一共%d种组合",count);
	getchar();
}
这个好,非递归解法,非常巧妙
赵4老师 2013-08-07
  • 打赏
  • 举报
回复
“给定一个小点的输入,完整单步跟踪(同时按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史)一遍。”是理解递归函数工作原理的不二法门! 递归函数关注以下几个因素 ·退出条件 ·参数有哪些 ·返回值是什么 ·局部变量有哪些 ·全局变量有哪些 ·何时输出 ·会不会导致堆栈溢出
JAYXIANJIAN 2013-08-07
  • 打赏
  • 举报
回复
貌似lz800年上一次论坛。。。
清天灵月 2013-04-02
  • 打赏
  • 举报
回复
引用 1 楼 KuaiPengFei_ 的回复:
/*很猥琐的代码!!!!!!*/ C/C++ code?12345678910111213141516171819202122232425262728293031#include <stdio.h> int main(){ int i1,i2,i3,i4,i5,i6; int a[10] = {1,2,3,4,5,6,7,8,10}; int su……
大神啊
快乐的2 2013-04-01
  • 打赏
  • 举报
回复
可以用背包算法实现,定义一个6大小的背包,10个物品每个大小是1,用算法去除所有可能行的物品索引值(在10个物品中的位置),然后和源数组中的数据已匹配就出来了
一刀切 2013-04-01
  • 打赏
  • 举报
回复
引用 38 楼 abcxyz402408139 的回复:
C/C++ code?12345678910111213141516171819202122232425262728293031#include <stdio.h>int a[1000],j[10000],t[1000],m,b,aa=4;int putout(){ for (b=0;b<m;b++){printf("%d ",t[b]);}……
哥们还是你的最容易懂,支持一个!
qianlv_ 2013-03-29
  • 打赏
  • 举报
回复
简单的深度优先搜索
GUITK 2013-03-29
  • 打赏
  • 举报
回复
java 实现: public class Combination6From10 { //统计total组合 private static int count = 0; public static void main(String[] args) { //递归生成组合 combination6(""); System.out.println(count); } private static void combination6(String base) { //达到六位数退出 if(base.length() >= 6) { count++; System.out.println(base); return; } //for循环递归 for (int start = 0; start < 10; start++) { if(base.length() < 6) { combination6(base + start); } } } }
发发问问 2013-03-28
  • 打赏
  • 举报
回复
都是大神啊……
千树之影 2013-03-25
  • 打赏
  • 举报
回复
引用 125 楼 cmlingyi 的回复:
LZ要显示10个数的全排列 那太多了吧
题目明明白白写着“组合”,你怎么看成“排列”的?
wylwyl1108 2013-03-22
  • 打赏
  • 举报
回复
组合的话 就是 C10 6 10*9*8*7*6*5/(1*2*3*4*5*6) 共有210种组合
煮咖啡 2013-03-20
  • 打赏
  • 举报
回复
谢谢分享谢谢分享
潇洒王子 2013-03-20
  • 打赏
  • 举报
回复
也表示看不懂!
cmlingyi 2013-03-20
  • 打赏
  • 举报
回复
LZ要显示10个数的全排列 那太多了吧 以下代码为 1~n这n数的第m个排列:
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
    int i,n,m,cc[10001];
    while(cin>>n>>m)
    {
    for(i=0;i<n;i++)
        cc[i]=i+1;
	m--;
    while(m--) next_permutation(cc,cc+n);
    for(i=0;i<n-1;i++)
        printf("%d ",cc[i]);
    printf("%d\n",cc[i]);
    }
    return 0;
}
赵4老师 2013-03-19
  • 打赏
  • 举报
回复
“给定一个小点的输入,完整单步跟踪(同时按Alt+7键查看Call Stack里面从上到下列出的对应从里层到外层的函数调用历史)一遍。”是理解递归函数工作原理的不二法门! 递归函数关注以下几个因素 ·退出条件 ·参数有哪些 ·返回值是什么 ·局部变量有哪些 ·全局变量有哪些 ·何时输出 ·会不会导致堆栈溢出
加载更多回复(95)

5,530

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 模式及实现
社区管理员
  • 模式及实现社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧