您的位置:首页 > 其它

week 8

2014-04-15 19:55 218 查看
用rand编写一个程序,出来一个随机数

/* Note:Your choice is C IDE */
#include "stdio.h"
#include "stdlib.h"
main()
{
int ia;
ia=rand();
printf("the random number is %d \n",ia);
}
随机输出1~45之间的数

/* Note:Your choice is C IDE */
#include "stdio.h"
#include "stdlib.h"
main()
{
int ia,ib;
ia=rand();
ib=ia%45+1;
printf("the random number is %d \n",ib);
}
随机输出10个数

/* Note:Your choice is C IDE */
#include "stdio.h"
#include "stdlib.h"
main()
{
int ia,ib,i;
for(i=0;i<10;i++)
{
ia=rand();
ib=ia%45+1;
printf("the random number is %d \n",ib);
}
}

随机输出10个没有重复数字的数

/* Note:Your choice is C IDE */
#include "stdio.h"
#include "stdlib.h"
#define N 25
main()
{
int ia,ib,i,j;
int students
;
for(i=0;i<N;i++)
{
ia=rand();
ib=ia%45+1;
if(i>0)/*因为第一个数不可能重复 */
{
for(j=0;j<=i-1;j++)
{
if(ib==students[j])
{
ib=rand()%45+1;
j=-1;/*j=-1,继续返回执行for循环*/
}
}
}
students[i]=ib;
printf("the random number is %d \n",ib);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: