首页 新闻 会员 周边 捐助

不重复随机数生成与排序问题

0
悬赏园豆:80 [已关闭问题]
<p class="MsoBodyTextIndent" style="text-align: left; line-height: 150%; text-indent: 24pt; margin: 0cm 0cm 0pt; mso-char-indent-count: 2.0; tab-stops: 291.6pt;" align="left"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt;">某烟草企业生产线系统中涉及生成一系列不重复的随机码,去除其中的素数(质数<span lang="EN-US"></span></span></p> <p class="MsoBodyTextIndent" style="text-align: left; line-height: 150%; text-indent: 24pt; margin: 0cm 0cm 0pt; mso-char-indent-count: 2.0; tab-stops: 291.6pt;" align="left"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt;">)随机码,最后对随机码进行排序(显示在屏幕上),随机码为<span lang="EN-US">7</span>位(非<span lang="EN-US">0</span>开头),程序对内存和硬盘的使用不限容量。<span lang="EN-US"></span></span></p> <p class="MsoBodyTextIndent" style="text-align: left; line-height: 150%; text-indent: 24pt; margin: 0cm 0cm 0pt; mso-char-indent-count: 2.0; tab-stops: 291.6pt;" align="left"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt;">下面我们把问题再归纳一下<span lang="EN-US">:</span></span></p> <p class="MsoBodyTextIndent" style="text-align: left; line-height: 150%; text-indent: 24pt; margin: 0cm 0cm 0pt 26.9pt; mso-char-indent-count: 2.0; tab-stops: 291.6pt;" align="left"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">输入</span><span style="line-height: 150%; font-size: 12pt;" lang="EN-US"><span style="font-family: Times New Roman;">:</span></span><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">无。</span><span style="line-height: 150%; font-size: 12pt;" lang="EN-US"></span></p> <p class="MsoBodyTextIndent" style="text-align: left; line-height: 150%; text-indent: 24pt; margin: 0cm 0cm 0pt 26.9pt; mso-char-indent-count: 2.0; tab-stops: 291.6pt;" align="left"><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">输出</span><span style="line-height: 150%; font-size: 12pt;" lang="EN-US"><span style="font-family: Times New Roman;">:</span></span><span style="line-height: 150%; font-family: 宋体; font-size: 12pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">在屏幕上显示排序后的随机码结果。</span><span style="line-height: 150%; font-size: 12pt;" lang="EN-US"></span></p> <p><span style="font-family: 宋体; font-size: 12pt; mso-font-kerning: 1.0pt; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA;">其他<span lang="EN-US">:</span>程序需要使用<span lang="EN-US">C#</span>语言编</span></p>
憬井^_^~的主页 憬井^_^~ | 初学一级 | 园豆:120
提问于:2009-06-24 18:03
< >
分享
其他回答(2)
0
Code
static void Main(string[] args)
{
List
<Int32> list = new List<Int32>();
Random rand
= new Random();

do
{
int number = rand.Next(1000000, 9999999);

if (!list.Contains(number) && !isPrimeNumber(number))
{
list.Add(number);
}
}
while (list.Count < 100);

list.Sort();

foreach (int item in list)
{
Console.WriteLine(item);
}
}

private static bool isPrimeNumber(int number)
{
//返回 number是否是素数
}
zengshunyou | 园豆:345 (菜鸟二级) | 2009-06-24 21:11
0

传说Guid是不会重复的,并且Guid的HashCode也很少重复,所以先生成100个Guid的HashCode,然后再挑重复的,此时重复的可能性已经非常非常小了,挑去重复的再用新的Guid补上,效率可能会好些

Gray Zhang | 园豆:17610 (专家六级) | 2009-06-25 01:20
0

Code
using System.Collections;

class PrimeNumber
{
Dictionary
<int, int> _PrimeNumberDict = new Dictionary<int,int>();

public PrimeNumber(int maxNumber)
{
BitArray numbers
= new BitArray(maxNumber, true);

for (int i = 2; i < maxNumber; i++)
{
if (numbers[i])
{
for (int j = i * 2; j < maxNumber; j += i)
{
numbers[j]
= false;
}
}
}

for (int i = 1; i < maxNumber; i++)
{
if (numbers[i])
{
_PrimeNumberDict.Add(i, i);
}
}
}

public bool IsPrimeNumber(int n)
{
return _PrimeNumberDict.ContainsKey(n);
}
}


static void Main(string[] args)
{
PrimeNumber pn
= new PrimeNumber(9999999);

SortedDictionary
<int, int> list = new SortedDictionary<int, int>();
Random rand
= new Random();

do
{
int number = rand.Next(1000000, 9999999);

if (!pn.IsPrimeNumber(number))
{
if (!list.ContainsKey(number))
{
list.Add(number, number);
}
}
}
while (list.Count < 100);

foreach (int item in list.Keys)
{
Console.WriteLine(item);
}
}

eaglet | 园豆:17139 (专家六级) | 2009-06-25 08:12
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册