GPA

原题:

GPA

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3645 Accepted Submission(s): 1489
 
Problem Description
Each course grade is one of the following five letters: A, B, C, D, and F. (Note that there is no grade E.) The grade A indicates superior achievement , whereas F stands for failure. In order to calculate the GPA, the letter grades A, B, C, D, and F are assigned the following grade points, respectively: 4, 3, 2, 1, and 0.
 
Input
The input file will contain data for one or more test cases, one test case per line. On each line there will be one or more upper case letters, separated by blank spaces.
 
Output
Each line of input will result in exactly one line of output. If all upper case letters on a particular line of input came from the set {A, B, C, D, F} then the output will consist of the GPA, displayed with a precision of two decimal places. Otherwise, the message "Unknown letter grade in input" will be printed.
 
Sample Input
A B C D F
B F F C C A
D C E F
 
Sample Output
2.00
1.83
Unknown letter grade in input
 
Author
2006Rocky Mountain Warmup
 
Source
HDU “Valentines Day” Open Programming Contest 2009-02-14
 
Recommend
lcy

源代码:

 1 #include <iostream>
 2 #include <iomanip>
 3 #include <string>
 4 using namespace std;
 5 
 6 int main()    {
 7     double result;
 8     string bbb;
 9     int sum = 0, count = 0;
10     char aaa;
11     while (getline(cin, bbb))    {
12         bool judge = true;
13         for (int i = 0; i < bbb.length(); i++)    {
14             aaa = bbb[i];
15             switch (aaa)    {
16                 case 'A':{
17                     sum += 4;
18                     count++;
19                     break;
20                 }
21                 case 'B':{
22                     sum += 3;
23                     count++;
24                     break;
25                 }
26                 case 'C':{
27                     sum += 2;
28                     count++; 
29                     break;
30                 }
31                 case 'D':{
32                     sum += 1;
33                     count++;
34                     break;
35                 }
36                 case 'F':{
37                     count++;
38                     break;
39                 }
40                 case ' ':{
41                     break;
42                 }
43                 default:{
44                     judge = false;
45                     break;
46                 }
47             } 
48         } 
49         result = sum * 1.0 / count; // sum, count都是int型,一定记住在两个int型要转换成double型时,要乘1.0 
50         if (judge)    cout << fixed << setprecision(2) << result << endl; // setpresicion的单词要拼写对 
51         else    cout << "Unknown letter grade in input" << endl; 
52         sum = 0;    count = 0; //记得初始化为0 
53     }
54     return 0;
55 } 
原文地址:https://www.cnblogs.com/QingHuan/p/4272634.html