JAYLEE900 2022-08-22 10:37 采纳率: 37.5%
浏览 67
已结题

如何以递归形式如何实现下述代码功能

如何将以下代码以递归形式实现,并且展示以下递归算法的逻辑和意义

#include <iostream>
#include <vector>
using namespace std;
 
vector<vector<vector<int>>> records;
vector<vector<int>> len_records;
vector<int> datas = { 1,2,3,4,5 };
int len = 0;
 
 
int main()
{
    len = 1;
    for (int i = 0;i < datas.size();i++)
    {
        vector<int> temp;temp.push_back(datas[i]);
        len_records.push_back(temp);
    }
    records.push_back(len_records);
    len_records.clear();
 
 
    len = 2;
    for (int i = 0;i < datas.size();i++)
    {
        for (int j = i+1;j < datas.size();j++)
        {
            vector<int> temp;temp.push_back(datas[i]), temp.push_back(datas[j]), len_records.push_back(temp);
        }
    }
    records.push_back(len_records);
    len_records.clear();
 
 
    len = 3;
    for (int i = 0;i < datas.size();i++)
    {
        for (int j = i + 1;j < datas.size();j++)
        {
            for (int k = j + 1;k < datas.size();k++)
            {
                vector<int> temp;temp.push_back(datas[i]), temp.push_back(datas[j]), temp.push_back(datas[k]);len_records.push_back(temp);
            }
        }
    }
    records.push_back(len_records);
    len_records.clear();
 
 
    len = 4;
    for (int i = 0;i < datas.size();i++)
    {
        for (int j = i + 1;j < datas.size();j++)
        {
            for (int k = j + 1;k < datas.size();k++)
            {
                for (int l = k + 1;l < datas.size();l++)
                {
                    vector<int> temp;temp.push_back(datas[i]), temp.push_back(datas[j]), temp.push_back(datas[k]), temp.push_back(datas[l]);len_records.push_back(temp);
                }
            }
        }
    }
    records.push_back(len_records);
    len_records.clear();
 
 
    len = 5;
    for (int i = 0;i < datas.size();i++)
    {
        for (int j = i + 1;j < datas.size();j++)
        {
            for (int k = j + 1;k < datas.size();k++)
            {
                for (int l = k + 1;l < datas.size();l++)
                {
                    for(int q=l+1;q<datas.size();q++)
                    {
                        vector<int> temp;temp.push_back(datas[i]), temp.push_back(datas[j]), temp.push_back(datas[k]), temp.push_back(datas[l]), temp.push_back(datas[q]);len_records.push_back(temp);
                    }
                }
            }
        }
    }
    records.push_back(len_records);
    len_records.clear();
 
    for (int i = 0;i < records.size();i++)
    {
        for (int j = 0;j < records[i].size();j++)
        {
            for (int k = 0;k < records[i][j].size();k++)
            {
                cout << records[i][j][k] << ",";
            }
            cout << " ";
        }
        cout << endl;
    }
 
    system("pause");
 
    return 0;
}
 
 
 

  • 写回答

2条回答 默认 最新

  • 真相重于对错 2022-08-22 11:21
    关注
    #include <iostream>
    #include <vector>
    using namespace std;
    
    vector<vector<vector<int>>> records;
    vector<vector<int>> len_records;
    vector<int> datas = { 1,2,3,4,5 };
    int len = 0;
    void func(int pos,int n,vector<int>& nums) {
        if(n==0)
        {
            len_records.push_back(nums);
            return;
        }
        for(int i=pos;i<datas.size();i++){
            nums.push_back(datas[i]);
            func(i+1,n-1,nums);
            nums.pop_back();
            
        }
    }
    
    int main()
    {
        vector<int> tmp;
        for (int i = 1; i <= datas.size(); i++) {
            func(0,i,tmp);
        }
        for(auto& v: len_records){
            for(auto& n:v)
                cout<<n<<" ";
            cout<<endl;
            }
        return 0;
    }
    
    
    
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月31日
  • 已采纳回答 8月23日
  • 创建了问题 8月22日

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀