zoukankan      html  css  js  c++  java
  • 输出一个数组的全排列

    命题:

    将一个数组的全排列输出,数据无素不重复(暂不考虑重复的情况.)

    如:定一个这样一个数

    int a[] = new a[4]{1,2,3,4};

    输出结果:

    1234
    1243
    1324
    1342
    1423
    1432
    2134
    2143
    2314
    2341
    2413
    2431
    3124
    3142
    3214
    3241
    3412
    3421
    4123
    4132
    4213
    4231
    4312
    4321

     我现在只是假定数组数据是1234,原理是一样的。实现程序如下:

    代码如下(C#):

    代码
     1         int count = 0;
     2         List<int> list = new List<int>();
     3         int Max = 4;
     4         private void CalcCycle()
     5         {
     6             if (list.Count == 4)
     7             {
     8                 string text = "";
     9                 for (int j = 0; j < Max; j++)
    10                 { text += list[j].ToString(); }
    11                 Console.WriteLine(text + "\r\n");
    12                 count++//总数+1
    13             }
    14             else
    15             for (int i = 0; i < Max; i++)
    16             {
    17                 int a = i + 1;
    18                 if (!list.Contains(a))
    19                 {
    20                     list.Add(a);
    21                     CalcCycle();
    22                     list.Remove(a);
    23                 }
    24             }
    25         }
  • 相关阅读:
    为什么puppeteer比selenium好?
    Puppeteer
    js跳出多层循环
    webpack loader- 图片处理
    webpack的loader的原理和实现
    Webpack中Loader的pitch方法
    url-loader和file-loader区别
    Vue中强制组件重新渲染的正确方法
    ERR_RESPONSE_HEADERS_MULTIPLE_CONTENT_DISPOSITION :浏览器下载报错
    JSBridge的原理及使用
  • 原文地址:https://www.cnblogs.com/benwu/p/1690404.html
Copyright © 2011-2022 走看看