题解 | #中位数#

中位数

https://www.nowcoder.com/practice/2364ff2463984f09904170cf6f67f69a

#include <iostream>
#include<algorithm>
using namespace std;

int main() {
    int n;
    while (cin >> n&&n!=0) 
    { // 注意 while 处理多个 case
       int *a=(int *)malloc(sizeof(n));
       for(int i=0;i<n;i++)cin>>a[i];
       sort(a,a+n);
       //cout<<"n="<<n<<endl;
        if(n%2==1)
        {
            cout<<a[n/2]<<endl;
        }
         else 
       {
            int mid=(a[n/2-1]+a[n/2])/2;
            cout<<mid<<endl;
       }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

04-21 20:49
长沙学院 Java
wu970:标准北漂配置,怎么看着装修风格有点像自如的😭
点赞 评论 收藏
分享
UtopianYou...:这个简历排版真的不太行哦,去找免费的或者花点小钱,把排版弄整齐一点吧,看着舒服。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务