OPENJUDGE 4043 GPA排名系统

来源:互联网 发布:印度网络发达吗 编辑:程序博客网 时间:2024/05/22 17:35

变量定义:

1) totalSum : Summation of All avaliable scores

2) lineSum[i] : Summation of all avaliable scores in a single line

3) studentNum : student Number

4) courseNum : course Number

5) zeroCount : number of 0s in a single column

6) totalZero : counter of all 0s in the matrix

7) currentSum[i] : summation of all scores in column i

公式如下:

d[i] = {( totalSum - ∑<当前列中元素为零的行k>lineSum[k] ) / (studentNum-zeroCount)*courseNum - (totalZero-∑lineZero[k])} - currentSum[i] / (studentNum - zeroCount);

代码:

# include <iostream># include <string># include <vector>using namespace std;class CourseInfo{private:int** matrix;int totalSum;int totalZero;//Counter For Zerosvector<int> lineSum;vector<int> lineZero;int studentNum;int courseNum;vector<int> modification;vector<string> courseName;public:CourseInfo(int studentNum_, int courseNum_){int i = 0;int j = 0;matrix = new int*[studentNum_];for ( i = 0; i < studentNum_; i++ ){matrix[i] = new int[courseNum_];}for ( i = 0; i < studentNum_;i++ ){for ( j = 0; j < courseNum_; j++ ){matrix[i][j] = 0;}}studentNum = studentNum_;courseNum = courseNum_;totalSum = 0;totalZero = 0;}void calculateSum(){int sum = 0;int sumT = 0;int zero = 0;int zeroT = 0;int j = 0;for ( int i = 0; i < studentNum; i++ ){sum = 0;zero = 0;for ( j = 0; j < courseNum; j++ ){sum += matrix[i][j];if ( matrix[i][j] == 0 ){zero++;}}lineSum.push_back(sum);lineZero.push_back(zero);sumT += sum;zeroT += zero;}totalSum = sumT;totalZero = zeroT;}void getInput(){string name;int i = 0;int j = 0;for ( i = 0; i < courseNum; i++ ){cin >> name;courseName.push_back(name);}for ( i = 0; i < studentNum; i++ ){for ( j = 0; j < courseNum; j++ ){cin >> matrix[i][j];}}}void modify(){int d = 0;int currentSum = 0;int zeroCount = 0;int total_0 = totalZero;int total_Sum = totalSum;int j = 0;for ( int i = 0; i < courseNum; i++ ){d = 0;currentSum = 0;zeroCount = 0;total_0 = totalZero;total_Sum = totalSum;for ( j = 0; j < studentNum; j++ ){currentSum += matrix[j][i];if ( matrix[j][i] == 0 ){zeroCount++;total_0 -= lineZero[j];total_Sum -= lineSum[j];}}d = total_Sum/((studentNum-zeroCount)*courseNum-total_0) - currentSum/(studentNum-zeroCount);modification.push_back(d);}}void getOutput(){for ( int i = 0; i < courseNum; i++ ){cout << courseName[i] << " " << modification[i] << endl;}}};int main(){int m = 0;int n = 0;cin >> m >> n;CourseInfo info(m,n);info.getInput();info.calculateSum();info.modify();info.getOutput();return 0;}



原创粉丝点击