1-10 | 分享 随机产生1000个数,并分屏显示(每行显示10个数字,每页显示10行),而且在

1-10 | 分享
随机产生1000个数,并分屏显示(每行显示10个数字,每页显示10行),而且在每一屏的下方显示本屏中数据的最大值、最小值和平均值。
提示:循环显示,在分屏点上输出press any key to continue…,通过getch()函数让用户以按回车键的方式进入下一屏

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

int main()

{int sum,max,min,x,i,j;

 srand(time(NULL));

 for(i=0;i<10;i++)

 {

system("cls");

max=-1;

min=10000;

sum=0;

for(j=0;j<100;j++)

{

x=rand()%1000+1;

sum+=x;

if(x>max)max=x;

if(x<min)min=x;

printf("%6d",x);

if(j%10==9)printf("\n");

}

printf("\nMax=%d  Min=%d  Aver=%.2f\n",max,min,sum/100.0);

printf("press any key to continue...");

getch();

 }

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-01-11
#include<iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
int main(){
int i,j,r,k=0,min,max,sum;
srand(time(0));
while(k<10){
min=999;max=0;sum=0;//在每一个循环中重新设置三个初始值
for(i=0;i<10;i++){
for(j=0;j<10;j++)
{
r=rand()%1000;
cout<<r<<" ";
if(r<min) min=r;
if(r>max) max=r;
sum+=r;
}
cout<<endl;
}
cout<<"max="<<max<<" "<<"min="<<min<<" "<<"average="<<sum/100<<endl;
cout<<"press any key to continue… "<<endl;
k++;
getchar();
system("cls");
}
return 0;
}
第2个回答  推荐于2017-09-30
//#include "stdafx.h"//If the vc++6.0, with this line.
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "time.h"
int main(void){
    int n,max,min,i,k,x,sum;
    srand((unsigned)time(NULL));
    for(n=0;n<10;n++){
        for(sum=k=i=0;i<100;i++){
            x = i ? rand() : max=min=rand();
            printf(++k%10 ? "%7d" : "%7d\n",x);
            if(max<x) max=x;
            if(min>x) min=x;
            sum+=x;
        }
        printf("MAX: %d MIN: %d AVER: %g\n\n",max,min,sum/100.0);
        printf("%s\n","press any key to continue…");
        getch();
    }
    return 0;
}

相似回答