输出数组的全排列,用c++实现。

这是一个C++程序,用于输入数组并进行全排列操作。程序首先通过Input函数获取用户输入的整数数组,直到遇到0为止。然后,使用 Permution 函数生成所有可能的排列组合,并通过 Output 函数打印。整个过程通过count1计数排列次数。程序在2020年10月14日的数据结构课程实验中编写,展示了基本的数组操作和递归全排列实现。
摘要由CSDN通过智能技术生成

// DataStructure01.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//本次程序是20201014第一次数据结构课程实验,未来可期,加油努力

#include <iostream>
using namespace std;
int count1 = 0;

void Output(int* outOb, int size);//问题 用for循环代替while循环 在方法的参数列表中加入一个size使其避免超界
int Input(int* ob) {
    cout << "Input" << endl;
    for (int i = 0; i < 20; i++) {
        cin >> ob[i];
        if (ob[i] == 0) return i - 1;
    }
    return 19;
}; //注意size是下表而非大小
void Output(int* outOb,int size) {
    for (int i = 0; i < size; i++) 
    {
        cout << outOb[i]<<",";
        }
    cout << outOb[size];
}
void SwapElement(int& one, int& theOther)
{
    int temp1;
    temp1 = one;
    one = theOther;
    theOther = temp1;
}
void End0();
void Permution(int* ob, int m1, int n1,int size) {
    if (m1 == n1 )
    {
        count1++;
        Output(ob, size);//要用到的都需要通过函数列表传过来
        cout << endl;
    }
    for (int i = m1; i <=n1; i++) {
        if (i == m1)//i==m1时无需交换数组的两个元素
            Permution(ob, m1 + 1, n1, size);
        else {
            SwapElement(ob[i], ob[m1]);
            Permution(ob, m1 + 1, n1,size);
            SwapElement(ob[i], ob[m1]);

        }
    }
}

int main()
{
    int Object[20];
    int size = 0;
    size=Input(Object);
   
    cout << "Output" << endl;
    Permution(Object, 0, size, size);
    cout << "end";
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值