計算每次輸入一個數之後,新的中位數是多少

[心得]

1.排序用 insertion sort,往前插入到適當的位置 (因為前面都已經排序完成)

 

#include <stdio.h>

int main()
{
    int x;
    int input[10000];
    int n=0;
    int i,temp;
   
    while(scanf("%d",&x)!=EOF)
    {
        n++;
        input[n]=x;
       
        for(i=n;i>=1;i--)   
            if(input[i]<input[i-1])   
            {
                temp=input[i] ;
                input[i]=input[i-1];
                input[i-1]=temp;
            }
       
        if(n%2==1)
            printf("%d\n",input[(n+1)/2]);
        else
            printf("%d\n",(input[n/2]+input[n/2+1])/2);
    }
       
    return 0;
}

arrow
arrow
    全站熱搜

    RingsACM 發表在 痞客邦 留言(0) 人氣()