| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 565 人关注过本帖
标题:递归高手都来帮忙!!!!!
只看楼主 加入收藏
morose2004
Rank: 1
等 级:新手上路
帖 子:17
专家分:0
注 册:2011-5-11
结帖率:66.67%
收藏
 问题点数:0 回复次数:0 
递归高手都来帮忙!!!!!
8选5的组合
高手给详细解析下下面的程序是如何用递归思想的?
#include <iostream>
 #include <vector>
 using namespace std;
 
int iarray[8] = {1, 2, 3, 4, 5, 6, 7, 8};
 vector<vector<int> > vvec; //存放组合结果
 int existFlag[8] = {0};
 
void   zuhe(int m)
 {
 if(m>8)
 {
    return;
 }
 
int count = 0;
 for(int i=0; i<8; ++i)
 {
    count = count + existFlag[i];
 }
 if( count < 5)
 {
    for(int i=1; i>=0; --i)
    {
     existFlag[m] = i;
     zuhe(m+1);
    }
 }
 else
 {
    vector<int> ivec;
    for(int i=0; i<8; ++i)
    {
     if( existFlag[i] == 1 )
     {
      ivec.push_back(iarray[i]);
     }
    }
    vvec.push_back(ivec);
 }
 }
 
int main()
 {
 zuhe(0);
 cout << vvec.size() << endl;
 for(int i=0; i<vvec.size(); ++i)
 {
    for(int j=0; j<vvec[i].size(); ++j)
    {
     cout << vvec[i][j] <<" ";
    }
    cout << endl;
 }
 return 0;
 }
搜索更多相关主题的帖子: return 
2011-05-13 21:13
快速回复:递归高手都来帮忙!!!!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013537 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved