首页 > 代码库 > Median-求中位数,文件中数字升序排列
Median-求中位数,文件中数字升序排列
Median-求中位数,文件中数字升序排列:
//Median-求中位数,文件中数字升序排列 #include<iostream> #include<fstream> #include<cstdlib> int main() { using namespace std; ifstream fin; ofstream fout; double median,tem1,tem2; int count = 1; fin.open("numbers.dat"); if(fin.fail()) { cout<<"Input file opening failed.\n"; exit(1); } fin>>tem1; while(!fin.eof()) { count++; fin>>tem1; } cout<<count<<endl; fin.close(); fin.open("numbers.dat"); if(fin.fail()) { cout<<"Input file opening failed.\n"; exit(1); } if(count % 2) for(int i = 1;i <= (count/2+1);i++) fin>>median; else { for(int i = 1;i <= (count/2);i++) { fin>>tem1; } fin>>tem2; //cout<<tem1<<endl; //cout<<tem2<<endl; median = (tem1+tem2)/2; } cout<<"The median of the numbers is "<<median<<endl; fin.close(); return 0; }
文件1:
1 2 3 4 5 6
结果1:
6 The median of the numbers is 3.5
文件2:
1 2 3 4 5 6 7
结果2:
7 The median of the numbers is 4
Median-求中位数,文件中数字升序排列
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。