HDOJ 4802 GPA

Problem Description
In college, a student may take several courses. for each course i, he earns a certain credit (c i), and a mark ranging from A to F, which is comparable to a score (s i), according to the following conversion table

The GPA is the weighted average score of all courses one student may take, if we treat the credit as the weight. In other words,

An additional treatment is taken for special cases. Some courses are based on “Pass/Not pass” policy, where stude nts earn a mark “P” for “Pass” and a mark “N” for “Not pass”. Such courses are not supposed to be considered in computation. These special courses must be ignored for computing the correct GPA.
Specially, if a student’s credit in GPA computation is 0, his/her GPA will be “0.00”.
 

 

Input
There are several test cases, please process till EOF.
Each test case starts with a line containing one integer N (1 <= N <= 1000), the number of courses. Then follows N lines, each consisting the credit and the mark of one course. Credit is a positive integer and less than 10.
 

 

Output
For each test case, print the GPA (rounded to two decimal places) as the answer.
 

 

Sample Input
5 2 B 3 D- 2 P 1 F 3 A 2 2 P 2 N 6 4 A 3 A 3 A 4 A 3 A 3 A
 

 

Sample Output
2.33 0.00 4.00

思路:输入数据后进行判断,如果是标准的GPA,则返回相应的数据,并用一个全局变量来表示是否全部都是非法的GPA,如果是则直接输出0即可

 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 int flag=0;//全局变量,用来标识是否全是P或者N
 5 float table(char *p)
 6 {
 7     //flag=0;
 8     if(p[0]=='P'||p[0]=='N')
 9         return 0.0;
10     else 
11     {
12         if(p[0]=='A'&&p[1]!='-')
13         {flag++;return 4.0;}
14         else if(p[0]=='A'&&p[1]=='-')
15         {flag++;return 3.7;}
16         else if(p[0]=='B'&&p[1]=='+')
17             {flag++;return 3.3;}
18         else if(p[0]=='B'&&p[1]!='-'&&p[1]!='+')
19         {flag++;return 3.0;}
20         else if(p[0]=='B'&&p[1]=='-')
21         {flag++;return 2.7;}
22         else if(p[0]=='C'&&p[1]=='+')
23         {flag++;return 2.3;}
24         else if(p[0]=='C'&&p[1]!='-'&&p[1]!='+')
25         {flag++;return 2.0;}
26         else if(p[0]=='C'&&p[1]=='-')
27         {flag++;return 1.7;}
28         else if(p[0]=='D'&&p[1]!='-')
29         {flag++;return 1.3;}
30         else if(p[0]=='D'&&p[1]=='-')
31         {flag++;return 1.0;}
32         else {flag++;return 0.0;}
33     }
34 }
35 int main()
36 {
37     int n=0;
38     int i=0;
39     float GPA=0.0;
40     int c;
41     int ci=0;
42     char s[2]={0};
43     while(scanf("%d",&n)!=EOF)
44     {
45         GPA=0.0;
46         ci=0.0;
47         flag=0;
48         for(i=0;i<n;i++)
49         {
50             cin>>c>>s;
51             GPA+=((float)c)*table(s);
52             if(!(s[0]=='P'||s[0]=='N'))
53                 ci+=c;
54         }
55         if(flag==0)
56             printf("0.00\n");
57         else
58         {
59             GPA=GPA/(float)ci;
60             printf("%.2f\n",GPA);
61         }
62     }
63 }

 

转载于:https://www.cnblogs.com/kb342/p/3740693.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值