我想用C# 里的Random 写一个随机数排序不重复的程序,请高手帮忙修改一下!!!急急 20
publicstaticvoidMain(){inta=0;int[]arr=newint[6];Randomr=newRandom();for(inti=0;i<arr...
public static void Main()
{
int a = 0;
int[] arr = new int [6];
Random r = new Random();
for (int i = 0; i < arr.Length-1; i++)
{
arr[i] = r.Next(1, 33);
for (int j = arr[i]; j < arr.Length; j++)
{
if (arr[i] < arr[j])
{
a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
} 展开
{
int a = 0;
int[] arr = new int [6];
Random r = new Random();
for (int i = 0; i < arr.Length-1; i++)
{
arr[i] = r.Next(1, 33);
for (int j = arr[i]; j < arr.Length; j++)
{
if (arr[i] < arr[j])
{
a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
} 展开
3个回答
展开全部
static void Main(string[] args)
{
int temp = 0;
int[] arr = new int[6];
Random r = new Random();
int index = 0;
while (index < 6)
{
temp = r.Next(1, 33);
if (!arr.Contains(temp))
{
arr[index] = temp;
index++;
}
}
for (int i = 0; i < arr.Length; i++)
{
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[i] > arr[j])
{
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
}
{
int temp = 0;
int[] arr = new int[6];
Random r = new Random();
int index = 0;
while (index < 6)
{
temp = r.Next(1, 33);
if (!arr.Contains(temp))
{
arr[index] = temp;
index++;
}
}
for (int i = 0; i < arr.Length; i++)
{
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[i] > arr[j])
{
temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Random()函数要加Thread.Sleep()才能真正随机。
using System.Threading;
public static void Main()
{
int a = 0;
int[] arr = new int [6];
for (int i = 0; i < arr.Length; i++)
{
Random r = new Random();
Thread.Sleep(5);
arr[i] = r.Next(1, 33);
for(int j = 0;j < i; j++)
{
if (arr[i] < arr[j])
{
a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
else
{
if(arr[i] == arr[j])
{
i--;
}
}
}
}
}
using System.Threading;
public static void Main()
{
int a = 0;
int[] arr = new int [6];
for (int i = 0; i < arr.Length; i++)
{
Random r = new Random();
Thread.Sleep(5);
arr[i] = r.Next(1, 33);
for(int j = 0;j < i; j++)
{
if (arr[i] < arr[j])
{
a = arr[i];
arr[i] = arr[j];
arr[j] = a;
}
else
{
if(arr[i] == arr[j])
{
i--;
}
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先生成随机序列再排序简单点
static void Test(int[] s)
{
int tmp;
for (int i = 0; i < s.Length; i++)
{
for (int j = i + 1; j < s.Length; j++)
{
if (s[i]>s[j])
{
tmp=s[j];
s[j]=s[i];
s[i]=tmp;
}
}
}
}
static void Test(int[] s)
{
int tmp;
for (int i = 0; i < s.Length; i++)
{
for (int j = i + 1; j < s.Length; j++)
{
if (s[i]>s[j])
{
tmp=s[j];
s[j]=s[i];
s[i]=tmp;
}
}
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询