#include
void main()
{
int i,a[1000],n,k=0;
scanf("%d",&n);
printf("生成1000以内的随机数: ");
for(i=0;i{
a[i]=("%d",rand()%1000);
printf("%d ",a[i]);
if(a[i]%2==0) k++;
}
printf("\n其中偶数的个数为%d",k);
}
运行示例:
// Test.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "stdlib.h"
#include "time.h"
int _tmain(int argc, _TCHAR* argv[])
{
int num, count = 0;
srand((unsigned int)time(0));
printf("请输入获取随机数个数:");
scanf_s("%d", &num);
getchar();
printf("获取随机数如下:\n");
//循环获取随机数
for (int i = 0; i < num; i++){
int tmp = rand() % 1000 + 1;//获取1-1000的随机数
printf("%d\n", tmp);
if (tmp % 2 == 0)//判断是否是偶数
count++;
}
printf("该组随机数内偶数个数为%d", count);
getchar();
return 0;
}
#include
#include "stdlib.h"
#include "time.h"
int main(int argc,char *argv[]){
int n,i,s;
scanf("%d",&n);
srand((unsigned)time(NULL));
for(s=i=0;is+=(rand()%1000&1);
printf("The result is %d\n",n-s);
return 0;
}