The calculation of GPA HDU--1202 pay attention to details

At the end of each semester, everyone is busy calculating their average grade, which is directly related to scholarship evaluation. Foreign universities calculate GPA (grade point average), also known as GPR (grade point ratio), which is the weighted average of grade points and credits to represent a student's performance. So how to calculate GPA?
Scoring method used by general universities

A 90-100 4 points
B 80-89 3 points
C 70-79 2 points
D 60-69 1 point
E 0-59 0 points

For example, if a student takes three courses, the subjects, credits and grades are: English: 3 credits, 92 points; Chemistry: 5 credits, 80 points; Mathematics: 2 credits, 60 points, then the GPA algorithm is as follows:
Subject credit score points score × points
English 3 92 4 12
Chemistry 5 80 3 15
Mathematics 2 60 1 2
Total 10 29 29/10=2.9
2.9 is the GPA of a certain student. Now, please write a program for calculating GPA.
Input contains multiple sets of data. The first row of each set of data has a number N, and each of the next N rows represents a score. There are two real numbers in each line, s, p, s represents the credits of this course, and p represents the student's grade (percentile system). If p=-1, it means that the student is absent from this course and should not be counted.
Output outputs one line for each group of data, which represents the student's GPA, with two decimal places. If GPA does not exist, output -1.
Sample Input3
3 92
5 80
2 60
Sample Output
2.90
This question requires the output result to retain two decimal places
First add the header file
#include<iomanip>//头文件
Add in the output code line
cout<<setiosflags(ios::fixed)<<setprecision(2)<<d<<endl;
Among them setiosflags(ios::fixed)<<setprecision(2) means to keep two decimal places.
If there is no setiosflags(ios::fixed) in the output line, it is the next line of code
cout<<setprecision(2)<<d<<endl;
It means to output two significant digits. And if the parameter number is less than the integer digits, the result is output in exponential form. (Note that the output results are all rounded results)
After solving the output, there is another thing worth paying attention to in this question is the process of converting scores to points. The data given in the question such as 80-89 points is 4 is actually not rigorous, because the input score is a real score, in this case 89.5 There will be no corresponding points. The error code is as follows:
double idian(double p)
{
    
    
 double dian;
 if(p>=90&&p<=100)
 dian=4.0;
 else if(p>=80&&p<89)
 dian=3.0;
 else if(p>=70&&p<=79)
 dian=2.0;
 else if(p>=60&&p<=69)
 dian=1.0;
 else
 dian=0;
 return dian;
}
This result must be WA. Therefore, in the function part, we must pay attention to the strictness of the judgment condition.

The title code is as follows:

#include<iostream>
#include<iomanip>
using namespace std;
double idian(double p)
{
    
    
 	double dian;
 	if(p>=90&&p<=100)
 	dian=4.0;
 	else if(p>=80&&p<90)
 	dian=3.0;
 	else if(p>=70&&p<80)
 	dian=2.0;
 	else if(p>=60&&p<70)
 	dian=1.0;
 	else
 	dian=0;
 	return dian;
}
int main()
{
    
    
 	int n;
 	while(cin>>n)
 	{
    
    
  		double xuesum=0;
  		double chengsum=0;
  		double s,p;
  		while(n--)
  		{
    
    
   			cin>>s>>p;//s是学分,p是成绩 
   			if(p!=-1)
   			{
    
    
   			 	xuesum+=s;
    				chengsum+=(idian(p)*s);
   			}
  		}
  		if(xuesum==0)
  		cout<<"-1"<<endl;
  		else
  		{
    
    
   			double d=chengsum/xuesum;
   			if(d==0)
   			cout<<"-1"<<endl;
   			else
   			cout<<setiosflags(ios::fixed)<<setprecision(2)<<d<<endl;
  		}
 	}
 	return 0;
} 

Guess you like

Origin blog.csdn.net/Huo6666/article/details/108074896