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

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

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

#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;
}



  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 8月30日
    • 创建了问题 8月22日

    悬赏问题

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