题目

Time Limit: 1000 ms
Case Time Limit: 1000 ms
Memory Limit: 128 MB
Total Submission: 72
Submission Accepted: 32

Description

上学期面对繁重的课程和考试,TYF终于考完了。成绩出来之后,TYF想看一下GPA(Grade Point Average,平均成绩点数)是多少,之后他熟练的打开了教务处,看到绩点显示0.00,他丝毫没有感到吃惊,他知道教务处又出现问题了。之后TYF决定自己写一个程序来计算自己的GPA。想必大家都知道GPA如何计算的,就是加权平均数。计算方式如下:
例如某学生的五门课程的学分和他所获得的绩点为:
A课程四个学分,绩点4;
B课程三个学分,绩点3;
C课程两个学分,绩点4;
D课程六个学分,绩点2;
E课程三个学分,绩点3。
以上五项成绩GPA为:
GPA=(44+33+24+62+3*3)/(4+3+2+6+3)=3.00
现在让你帮助TYF完成这项任务。

Input

多组输入,EOF结束
对于每组输入,第一行一个整数n (1<=n<=40),表示课程的数目。
接下来n行,每行两个数,一个整数ai(1<=ai<=10)表示一门课的学分,一个浮点数bi(0<=bi<=4.00),表示一门课的绩点。

Output

对于每组输入,输出一行, 为平均成绩点数(保留两位小数)。

Sample Input

5
4 4.0
3 3.0
2 4.0
6 2.0
3 3.0

Sample Output

3.00

题解

按照公式计算即可

代码

/*
By:OhYee
Github:OhYee
Email:oyohyee@oyohyee.com
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <vector>
#include <list>
#include <stack>
using namespace std;
 
#define REP(n) for(int o=0;o<n;o++)
 
int main() {
    int n;
    while(scanf("%d",&n) != EOF) {
        double ans1 = 0,ans2 = 0;
        double a,b;
        REP(n) {
            scanf("%lf%lf",&a,&b);
            ans1 += a*b;
            ans2 += a;
        }
        printf("%.2f\n",ans2 != 0 ? ans1 / ans2 : 0);
    }
    return 0;
}