gpt4 book ai didi

c++ - 通过 uva 的中值程序

转载 作者:行者123 更新时间:2023-11-28 06:18:20 24 4
gpt4 key购买 nike

这是我第一次使用在线判断,我尝试了一个简单的程序来熟悉环境。

这是 question .

我按照下面的方法解决了,但是得到了一个错误的答案!

#include<stdio.h>
#include<math.h>
#include<iostream>

int main()
{
int t;
int n;
int num[10];
int i,j,temp;
int s;
int fmid;
std::cin >>t;
int iter=0;
while (iter<t)
{
std::cin>>n;
if (n!=-1){
for(i=0;i<n;i++)
std::cin>>num[i];

for( i=0;i<n-1;i++)

for(j=i+1;j<n;j++)
if(num[i]>num[j])
{temp=num[i];
num[i]=num[j];
num[j]=temp;
}

s=0;
if (n%2 ==0)
{
int mid=n/2-1;
int midd=mid+1;

s=(num[mid]+num[midd])/2;
fmid=s;
}

else
{s=ceil(n/2);
fmid=num[s];}

std::cout<<fmid;
}
iter++;

}

return 0;

}

非常感谢任何建议。

谢谢

最佳答案

我会读取所有数字,将它们存储在一个数组中,然后使用 std::sort 对数组进行排序在 <algorithm>

请在下面找到我的代码:

#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>

int arr[10];

int main()
{
int N;
while ((std::cin >> N) && (N!=-1)){
for(int i=0;i<N;i++) {
std::cin >> arr[i];
}
std::sort(arr,arr+N);
if(N%2 == 1){
std::cout << arr[N/2] << std::endl;
}
else {
double ans = ((double)arr[N/2] + (double)arr[(N/2)-1]) / 2.0;
std::cout << ans << std::endl;
}
}

return 0;
}

希望对你有用。

我可以知道问题编号吗?我会尝试在那里提交并给你一个AC解决方案。

关于c++ - 通过 uva 的中值程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29807190/

24 4 0
文章推荐: css - 在移动设备上呈现的普通