1,请使用rand函数,要求一次输出10个学生的学号.
#include<stdio.h>
#include<stdlib.h>
main()
{
int i;
int j;
for(i=0;i<10;i++)
{
j=rand()%45+1;
printf("the %d number is %d \n",i+1,j);
}
}
2,
对1中的程序进一步改进,要求使用数组来存储这些学生的学号,同时要求学号不能重复。
#include "stdio.h"
#include "stdlib.h"
#define N 25
main()
{
int i,j,temp;
int students[N];
for(i=0;i<N;i++){
temp = rand()%45 +1 ;
if(i>0){
for(j=0;j<=i-1;j++){
if(temp == students[j])
{
temp = rand()%45 +1;
j=-1;
}
}
}
students[i] = temp;
printf("the %d student no is : %d\n",i,students[i]);
}
}
3, 定义一个100长度的整型数组,使用rand函数生产随机数对每个元素进行赋值,顺序遍历该数组,输出所有值大于50的数。
#include "stdio.h"
#include <stdlib.h>
#define N 100
void main()
{
int a[N];
int i,m;
for(i=0;i<N;i++)
{
a[i]=rand();
m=a[i];
if(m>50)
printf("a[%d]=%d\n",i,m);
}
}