产生30个不重复的随机数并放入数组
#include "stdafx.h"
#include <time.h>
#include <stdlib.h>
int _tmain(int argc, _TCHAR* argv[])
{
int a[30];
int n = 0;
int t;
int i;
srand(time(NULL));
while (1)
{
t = rand()%100;
for (i = 0; i < n; i++)
{
if (a[i] == t) //遍历数组中是否有重复的数
{
break;
}
}
if (i == n) //遍历后如果没有重复数,将值赋给数组
{
a[i] = t;
n++;
}
if (30 == n) //输出够30个数据后,退出
break;
}
for (int i = 0; i < 30; i++)
{
printf("%d\n", a[i]);
}
return 0;
}