中位数也就是中值;

故需要先对数组进行排序(选择,插入,冒泡排序),然后在找出数组的中值。

//求中值
#include<iostream>
using namespace std;

int Median(int a[],int N)
{
    int i,j,max;
    int t;
    for(i=0;i<N-1;i++)
    {
        max=i;
        for(j=i+1;j<N;j++)
        if(a[j]>a[max]) max=j;
        t=a[i];a[i]=a[max];a[max]=t;
    }
    return a[(N-1)/2];
 } 
 
 int main()
 {
     int a[]={1,2,4,7,3,9,10,11};

     cout<<Median(a,8)<<endl;
 }
 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-28
  • 2022-12-23
  • 2021-06-04
  • 2022-12-23
  • 2022-01-29
猜你喜欢
  • 2022-12-23
  • 2021-05-19
  • 2021-06-13
  • 2021-09-23
  • 2022-01-09
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案