大佬们,这道题咋解啊
文件score.txt中有某班级40人的成绩,将文件中的成绩读取出来。(1)对成绩进行排序,以5个一行在屏幕上输出。
(2)计算该班成绩的均方差:(最大值-最小值)/平均值 。
#include <iostream> #include <fstream> #include <vector> #include <algorithm> using namespace std; bool readF(vector<int> &vec, int &lineNumber) { int score = 0; ifstream infile; //输入流 infile.open("C:\\Users\\Ans\\Desktop\\score.txt", ios::in); if (!infile.is_open()) return false; while (!infile.eof()) // 未结束一直读 { infile >> score; vec.push_back(score); lineNumber++; } infile.close(); return true; } int main() { vector<int> v; int lineNumber = 0; readF(v, lineNumber); sort(v.begin(), v.end()); int max = 0, min = 0, all = 0; for (int i = 0; i < v.size(); i++) { all += v[i]; if (v[i] > max) max = v[i]; if (v[i] < min) min = v[i]; cout << v[i] << " "; if (!((i+1) % 5)) cout << endl; } cout << (max - min) / (all / lineNumber) << endl; return 0; }