用c++生成20个随机数,由大到小排列,找出所有大于平均值的数据并...
发布网友
发布时间:2024-10-08 03:45
我来回答
共1个回答
热心网友
时间:2024-10-14 00:08
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void sort(int a[],int n) {
int i,j,k,t;
for(i = 0; i < n - 1; ++i) {
k = i;
for(j = i + 1; j < n; ++j) {
if(a[k] > a[j]) k = j;
}
if(i != k) {
t =a[i];
a[i] = a[k];
a[k] = t;
}
}
}
int main() {
int i,n = 20;
int a[20];
srand((unsigned)time(NULL));
for(i = 0; i < n; ++i) {
a[i] = rand()%2000 + 1;
if(i && i % 10 == 0) printf("\n");
printf("%5d",a[i]);
}
printf("\n");
sort(a,n);
printf("最大:%d\n",a[n - 1]);
printf("最小:%d\n",a[0]);
printf("中间:%d\n",a[n/2]);
return 0;
}
热心网友
时间:2024-10-14 00:06
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void sort(int a[],int n) {
int i,j,k,t;
for(i = 0; i < n - 1; ++i) {
k = i;
for(j = i + 1; j < n; ++j) {
if(a[k] > a[j]) k = j;
}
if(i != k) {
t =a[i];
a[i] = a[k];
a[k] = t;
}
}
}
int main() {
int i,n = 20;
int a[20];
srand((unsigned)time(NULL));
for(i = 0; i < n; ++i) {
a[i] = rand()%2000 + 1;
if(i && i % 10 == 0) printf("\n");
printf("%5d",a[i]);
}
printf("\n");
sort(a,n);
printf("最大:%d\n",a[n - 1]);
printf("最小:%d\n",a[0]);
printf("中间:%d\n",a[n/2]);
return 0;
}