V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
mitong3269
V2EX  ›  C#

求助 c#的编程 很简单

  •  
  •   mitong3269 · 2019-12-22 21:35:30 +08:00 · 11657 次点击
    这是一个创建于 1579 天前的主题,其中的信息可能已经有所发展或是发生改变。

    从 n 个元素中取 n 个元素组成互不相同并且不重复的 n 位数 输入 n 个数字,并显示出能组成的所有互不相同的、不重复的 n 位数。(如输入 1、2、3 三个数字,则输出为 123,132,213,231,312,321 等六组答案;如输入 1、1、1、2 三个数 字,则输出为:1112,1121,1211,2111 等四组答案) 要用 c# 怎么 n 位不重复呢

    4 条回复    2019-12-22 23:32:44 +08:00
    mitong3269
        1
    mitong3269  
    OP
       2019-12-22 21:37:28 +08:00
    int n1, n2, n3, n4;
    {

    Console.WriteLine("请输入数字");
    Console.Write("n1=");
    n1 = Convert.ToInt16(Console.ReadLine());
    Console.Write("n2=");
    n2 = Convert.ToInt16(Console.ReadLine());
    Console.Write("n3=");
    n3 = Convert.ToInt16(Console.ReadLine());
    Console.Write("n4=");
    n4 = Convert.ToInt16(Console.ReadLine());
    if (n1!=n2&&n2!=n3&&n3!=n4) ;
    //Console.Write("n1,n2,n3,n4"(n1,n2,n3,n4));
    }
    我写了个 4 位数的也不行
    crella
        2
    crella  
       2019-12-22 22:51:50 +08:00 via Android
    提个思路

    首先新建 id 数组为 1, 2, 3....元素的个数

    然后算 id 的全排列,再给这个全排列去掉重复项。
    crella
        3
    crella  
       2019-12-22 22:53:27 +08:00 via Android
    算完 id 的全排列后,按照每个排列中的每个 id 对应的字符,新建一个字符串,得到一个包含很多字符串的数组。最后去掉这个数组里面的重复的字符串。
    mitong3269
        4
    mitong3269  
    OP
       2019-12-22 23:32:44 +08:00
    @crella 谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5756 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 02:13 · PVG 10:13 · LAX 19:13 · JFK 22:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.