您的位置:首页 > 职场人生

面试题:实现在一个长度为255的数组,为数组中每一项填入1-255之间的数并且保证不重复

2017-03-22 22:00 441 查看
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
int a[255];//存储获取到的随机数。
int f[255] = {0};//存储是否获取到过。
int n = 0; //计数器。

srand(time(NULL));//设置随机数种子。

while(n<255)
{
int m = rand()%255; //获取一个0~255的随机数。
if(f[m]) continue;//该数之前已经获取到过。
a[n++] = m;//将该数存入数组。
f[m] = 1;//标记该数已经获取过。
}

for(n = 0; n < 255; n++)//打印结果。
printf("%d\t",a
+1);

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐