一个排列组合算法, 生成四位数字的随机数

as86238582 2012-11-19 09:30:37
必须有abc, 0-9的四位随机数密码
例如 abc0
1abc
ab2c

abc摆放位置随机...没硬性规定.
用数组实现
...全文
617 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Teng_s2000 2012-11-19
  • 打赏
  • 举报
回复
threenewbee 2012-11-19
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            foreach (string s in All())
            {
                Console.WriteLine(s);
            }
        }

        static IEnumerable<string> All()
        {
            return Enumerable.Range(0, 40).SelectMany(x => new string[] { "abc", "acb", "bac", "bca", "cab", "cba" }.Select(y => y.Insert(x / 10, (x % 10).ToString())));
        }
    }
}
as86238582 2012-11-19
  • 打赏
  • 举报
回复
引用 2 楼 SQL77 的回复:
C# code?123456789101112131415161718192021222324252627282930313233343536using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace ConsoleApplication1{ ……
晕,需要abc 任意排列的情况下,在增加 0到9任意一位数字..产生一个4位数字的字典. abc 不需要固定位置.. 0到9,任意一位数字插入到abc中... 题目我写的很清楚啊. 0acb 1acb 2acb 3acb 4acb 5acb a9cb 7acb 8acb 9acb a9cb a1cb a9cb a3cb a4cb a5cb a6cb a7cb a8cb a9cb a9cb a9cb a9cb a9cb a9cb a9cb a9cb 例如这样.
as86238582 2012-11-19
  • 打赏
  • 举报
回复
引用 1 楼 caozhy 的回复:
C# code?12345678910111213141516171819202122232425using System;using System.Collections.Generic;using System.Linq;using System.Text; namespace ConsoleApplication1{ class Program { ……
晕,需要abc 任意排列的情况下,在增加 0到9任意一位数字..产生一个4位数字的字典.
Hauk 2012-11-19
  • 打赏
  • 举报
回复

            string str = "abc";
            Random rand = new Random();
            str = str.Insert(rand.Next(4), rand.Next(10).ToString());
            Console.WriteLine(str);
SQL77 2012-11-19
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int i=1;
            while (i <= 100)
            {
                Random r = new Random();
                int rand = r.Next(10);
                string s = "abc" + rand.ToString();
                char[] ch = s.ToCharArray();
                string result = "";
                while (result.Length < 4)
                {
                    Random randtemp = new Random();
                    int rtemp = randtemp.Next(ch.Length);
                    if (result.IndexOf(ch[rtemp].ToString()) == -1)
                    {
                        result += ch[rtemp].ToString();
                    }
                }
                Console.WriteLine(result);
                i++;
            }
            
            Console.ReadKey();
        }
    }
}
threenewbee 2012-11-19
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i < 100; i++)
            Console.WriteLine(RandNum());
        }

        static string RandNum()
        {
            var rnd1 = "abc".OrderBy(x => Guid.NewGuid()).ToArray();
            var r = new Random(Guid.NewGuid().GetHashCode());
            int rnd2 = r.Next(0, 4);
            int rnd3 = r.Next(0, 10);
            return new string(rnd1.Take(rnd2).ToArray()) + rnd3.ToString() + new string(rnd1.Skip(rnd2).ToArray());
        }
    }
}

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧