即使输入的数量最终是偶数数量的元素,我的代码也会跳过 if 检查。这可能是我在阅读的章节中错过或没有注意到的一些愚蠢的事情。感谢您查看它。
#include "std_lib_facilities.h"
using namespace std;
int main()
{
vector<double> num;
double numb;
while(cin>>numb)
num.push_back(numb);
sort(num.begin(), num.end());
if(num.size() <= 2)
cout << "Need more data." << endl;
else if (num[num.size()%2] == 0)//even?
{
cout << "Median is: " << num[num.size()/2] << endl;
cout << "Median is: " << num[(num.size()/2)+1] << endl;
}
else
cout << "Median is: " << num[num.size()/2] << endl;
keep_window_open();
}