作业帮 > 综合 > 作业

c语言acm水题不会弄Descriptionfind the median of several integersInp

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/09 19:45:00
c语言acm水题
不会弄
Description
find the median of several integers
Input
The first line of input is the number of test cases T(T
#include <stdio.h>
void fun(double a[],int m){
    int i,j,temp;
    for(i=1;i<m;i++)
        for (j=0; j<m-i; j++) {
            if(a[j]>a[j+1]){
                temp   = a[j];
                a[j]   = a[j+1];
                a[j+1] = temp;
            }
        }
}//冒泡排序

int main(){
    int n,m,i;
    double a[111];
    double ave;
    while (~scanf("%d",&n)) {
        while (n--) {
            scanf("%d",&m);
            for(i=0;i<m;i++){
                scanf("%lf",&a[i]);
            }
            fun(a,m);
            if(m%2==1){
                ave=a[m/2];
            }//是奇数
            else{
                ave=(a[m/2]+a[m/2-1])/2;
            }//是偶数
            if(int(ave)*1.0==ave)
                printf("%d\n",(int)ave);//是整数
            else
                printf("%.1lf\n",ave);//是小数
               
        }
    }
}