题意:计算(学分*成绩对应的点数)的和/学分和。
注意点:1.题目上成绩对应范围是80~89,代码中不能写>=80&&<=89,因为89.5算在里面。
2.GPA不能计算有两种情况,第一种是全部考试缺考,第二种是(学分*成绩对应的点数)的和为0,换句话说GPA不能为0。
#include<bits/stdc++.h>
using namespace std;
typedef pair<double,double> pp;
double cha(double a)
{
if(a>=90) return 4;
else if(a>=80) return 3;
else if(a>=70) return 2;
else if(a>=60) return 1;
else return 0;
}
int main()
{
int n;
while(cin>>n){
pp *arr = new pp[n];
double num=0,sum=0,sum_a=0;
for(int i=0;i<n;i++){
cin>>arr[i].first>>arr[i].second;
if(arr[i].second!=-1) {
num++;
sum_a+=arr[i].first;
sum+=arr[i].first*cha(arr[i].second);
}
}
//cout<<sum<<' '<<sum_a<<endl;
if(num == 0||sum == 0) cout<<-1<<endl;
else printf("%.2lf\n",sum/sum_a);
}
return 0;
}