查找中间数C++

来源:百度知道 编辑:UC知道 时间:2024/05/18 05:01:51
给出n(1<=n<10000)个正整数,n为奇数,请对这n个数进行排序,找到中间的数字并输出
#include <iostream>
using namespace std;

int main()
{
int n,a[10000],i,k=0,j;
cin >> n;
while(n % 2 == 0)
{
cout << endl;
}
for(i = 0;i<= n - 1;i++)
{
cin >> a[i];
}
for(j = 0;j < n - 1 ;j++)
for(i = 0;i < n -1 -j;i++)
if(a[i]>a[i+1])
{
a[k] = a[i];
a[i]=a[i+1];
a[i+1]=a[k];
}

cout << a[i/2] << endl;

return 0;
}
哪错了

/*主要是for循环的问题,我已经改了,已经可以正常运行并实现你的功能了,但是我有个疑问,你为什么要在输入偶数的时候进行while循环,这样程序将无法终止这个循环?我有个想法,在while语句中加入这样一句“n--;”,这样如果用户输入偶数,则程序自动将此偶数改为小1的奇数(如输入6则程序将n改为5继续执行后面的程序,你觉得如何?)*/
#include <iostream>
using namespace std;

int main()
{
int n,a[10000],i,temp,j;
cin >> n;
while(n % 2 == 0)
{
cout << endl;
}
for(i = 0;i<= n - 1;i++)
{
cin >> a[i];
}
for(j = 0;j < n - 1 ;j++)
for(i = j+1;i < n ;i++)
if(a[j]>a[i])
{
temp = a[i];
a[i]=a[j];
a[j]=temp;
}

cout << a[n/2] << endl;

return 0;
}

while(n % 2 == 0)
{
cout << endl;
}

while 改为 if