c语言数组题目怎么做?
发布网友
发布时间:2022-04-20 08:01
我来回答
共4个回答
热心网友
时间:2023-06-16 19:20
灞曞紑鍏ㄩ儴#include <stdio.h>
int main()
{
int a[20];
int i,j;
int sum, min, max;
sum = 0;
for(i = 0; i < 20; i ++)
{
scanf("%d",a+i);
sum+=a[i];
}
for(i = 0; i < 20; i ++)
printf("%d ", a[i]);
printf("\n");
for(i = 0; i < 19; i ++)
{
for(j = i+1; j < 20; j ++)
if(a[i]<a[j])
{
min=a[i];
a[i]=a[j];
a[j] = min;
}
}
min=a[19];
max=a[0];
printf("sum=%d ave=%f, min=%d max=%d\n", sum, sum/20.0, min, max);
for(i = 0; i < 20; i ++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
热心网友
时间:2023-06-16 19:21
灞曞紑鍏ㄩ儴#include <stdio.h>
#define CNT 20
void mysort(int *a, int n)
{
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - 1 - i; j++) {
if (a[j] < a[j + 1]) {
int t = a[j + 1];
a[j + 1] = a[j];
a[j] = t;
}
}
}
}
int main(void)
{
int i;
int sum = 0;
int a[CNT];
for (i = 0; i < CNT; i++) {
scanf("%d", &a[i]);
sum += a[i];
}
mysort(a, CNT);
printf("sum=%d, avg=%f, min=%d, max=%d\n", sum, (float)sum / CNT, a[CNT - 1], a[0]);
for (i = 0; i < CNT; i++)
printf("%d ", a[i]);
printf("\n");
return 0;
}
热心网友
时间:2023-06-16 19:21
灞曞紑鍏ㄩ儴#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int s[20];
int i,j,sum=0,max=50,min=50,temp;
float avg;
srand((unsigned)time(0));
for(i=0;i<20;i++)
{
s[i]=rand()%100+1;
sum+=s[i];
if(max<s[i])
{
max=s[i];
}
if(min>s[i])
{
min=s[i];
}
printf("%2d ",s[i]);
}
avg=sum/20.0;
for(i=0;i<19;i++)
{
for(j=0;j<19-i;j++)
{
if(s[j]<s[j+1])
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}
printf("\n鎺掑簭鍚庯細\n");
for(i=0;i<20;i++)
{
printf("%2d ",s[i]);
}
printf("\navg=%.2f min=%d max=%d",avg,min,max);
return 0;
}
热心网友
时间:2023-06-16 19:22
灞曞紑鍏ㄩ儴#include <bits/stdc++.h>
using namespace std;
int a[31];
int main(){
for (int i=1;i<=20;i++) a[i]=rand(),printf("%d ",a[i]);
int sum=0,mini=1e9,maxi=0;
for (int i=1;i<=20;i++)
sum+=a[i],mini=min(mini,a[i]),maxi=max(maxi,a[i]);
printf("%.3lf %d %d\n",1.0*sum/20,mini,maxi);
for (int i=1;i<=20;i++) a[i]*=-1;
sort(a+1,a+21);
for (int i=1;i<=20;i++) printf("%d\n",-a[i]);
}