Problem Description
In college, a student may take several courses. for each course i, he earns a certain credit (c), and a mark ranging from A to F, which is comparable to a score (s), according to the following conversion table
HDOJ 4802 GPA-LMLPHP
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,
HDOJ 4802 GPA-LMLPHP
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即可

 #include <iostream>
#include <string>
using namespace std;
int flag=;//全局变量,用来标识是否全是P或者N
float table(char *p)
{
//flag=0;
if(p[]=='P'||p[]=='N')
return 0.0;
else
{
if(p[]=='A'&&p[]!='-')
{flag++;return 4.0;}
else if(p[]=='A'&&p[]=='-')
{flag++;return 3.7;}
else if(p[]=='B'&&p[]=='+')
{flag++;return 3.3;}
else if(p[]=='B'&&p[]!='-'&&p[]!='+')
{flag++;return 3.0;}
else if(p[]=='B'&&p[]=='-')
{flag++;return 2.7;}
else if(p[]=='C'&&p[]=='+')
{flag++;return 2.3;}
else if(p[]=='C'&&p[]!='-'&&p[]!='+')
{flag++;return 2.0;}
else if(p[]=='C'&&p[]=='-')
{flag++;return 1.7;}
else if(p[]=='D'&&p[]!='-')
{flag++;return 1.3;}
else if(p[]=='D'&&p[]=='-')
{flag++;return 1.0;}
else {flag++;return 0.0;}
}
}
int main()
{
int n=;
int i=;
float GPA=0.0;
int c;
int ci=;
char s[]={};
while(scanf("%d",&n)!=EOF)
{
GPA=0.0;
ci=0.0;
flag=;
for(i=;i<n;i++)
{
cin>>c>>s;
GPA+=((float)c)*table(s);
if(!(s[]=='P'||s[]=='N'))
ci+=c;
}
if(flag==)
printf("0.00\n");
else
{
GPA=GPA/(float)ci;
printf("%.2f\n",GPA);
}
}
}
05-11 19:26