我是初学者.求大侠指教.
我是初学者.求大侠指教.
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int i, x, j, a[20];
j=0;
printf("Ten random numbers from 0 to 99:");
for(i=0; i<20; i++)
{
a[i]=rand()%100;
printf(" %d ", a[i]);
}
printf("Please Input the number you want to know how many times it appeared:\n");
scanf("%d",&x);
for(i=0;i<20;i++)
{if(x==a[i]) ++j;}
printf("It appeared %d times",j);
return 0;
}
VC++运行成功,以后最好是自己想想,多想才有提高
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
int i, x, j, a[20];
j=0;
printf("Ten random numbers from 0 to 99:");
for(i=0; i<20; i++)
{
srand(time(NULL)); /*以便使下次运行的数也是随机,而不是总是同一批数*/
a[i]=rand()%100;
printf(" %d ", a[i]);
}
printf("Please Input the number you want to know how many times it appeared:\n");
scanf("%d",&x);
for(i=0;i<20;i++)
{if(x==a[i]) ++j;}
printf("It appeared %d times",j);
return 0;
}
VC++运行成功,以后最好是自己想想,多想才有提高
time(NULL)返回的是一正数长整型,会自动转换成无符号类型,不过,我还是得承认你这样是较好的。