定义一个10位的数组,array={1,2,3,4,5,6,7,8,9,10},怎样实现输出来是随机排列的
2个回答
展开全部
//利用随机数
# include "stdio.h"
# include "stdlib.h"
int main(void)
{
int array[]={1,2,3,4,5,6,7,8,9,10};
int i,j;
for (i=0; i<=9; ++i)
{
j = rand() % 10;
printf("%d ",array[j]);
}
printf("\n");
return 0;
}
/*
输出结果:
------------------------
2 8 5 1 10 5 9 9 3 5
Press any key to continue
------------------------------
*/
# include "stdio.h"
# include "stdlib.h"
int main(void)
{
int array[]={1,2,3,4,5,6,7,8,9,10};
int i,j;
for (i=0; i<=9; ++i)
{
j = rand() % 10;
printf("%d ",array[j]);
}
printf("\n");
return 0;
}
/*
输出结果:
------------------------
2 8 5 1 10 5 9 9 3 5
Press any key to continue
------------------------------
*/
追问
随机出的数不能重复
追答
# include "stdio.h"
# include "stdlib.h"
int main(void)
{
int array[]={1,2,3,4,5,6,7,8,9,10};
int i,j;
for (i=0; i<=9; ++i)
{
j = rand() % (10-i);
printf("%d ",array[j]);
int k;
for(k=j;k<10;++k)
array[k]=array[k+1];
}
printf("\n");
return 0;
}
/*
输出结果:
------------------------
2 10 8 7 9 6 4 1 3 5
Press any key to continue
------------------------------
*/
来自:求助得到的回答
展开全部
必须是1到10的不重复数吗?
Sub test()
Dim arr(1 To 10)
Dim d As Object
Set d = CreateObject("scripting.dictionary")
Do While x < 10
i = Int(Rnd() * 10 + 1)
If Not d.exists(i) Then
x = x + 1
d(i) = x
arr(x) = i
End If
Loop
Range("A1").Resize(10, 1) = Application.Transpose(arr)
End Sub
Sub test()
Dim arr(1 To 10)
Dim d As Object
Set d = CreateObject("scripting.dictionary")
Do While x < 10
i = Int(Rnd() * 10 + 1)
If Not d.exists(i) Then
x = x + 1
d(i) = x
arr(x) = i
End If
Loop
Range("A1").Resize(10, 1) = Application.Transpose(arr)
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询