2399:GPA
来源:互联网 发布:apache httpd安装配置 编辑:程序博客网 时间:2024/04/30 01:19
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 FB F F C C AD C E F
Sample Output
2.001.83Unknown letter grade in input
分析:
即使提示“Unknown letter grade in input”的信息,仍能进行后续输入,而不是程序到此结束。
代码如下:
#include <iostream>#include <iomanip>#include <string>using namespace std;int main(){char grade;string str;while (getline(cin,str))//getline按行读取{int sum = 0,k = 0,sign = 0;int i = 0;while (i < str.length()){grade = str[i++];switch (grade){case 'A':sum += 4;k++;break;case 'B':sum += 3;k++;break;case 'C':sum += 2;k++;break;case 'D':sum += 1;k++;break;case 'F':sum += 0;k++;break;case ' ':break;default:sign = 1;break;}}if(sign == 1)cout << "Unknown letter grade in input" << endl;elsecout << fixed << setprecision(2) << (sum * 1.0 / k) << endl;//设置小数点后的精度为2}return 0;}
- 2399:GPA
- hdu 2399 GPA
- hdu 2399 GPA
- hdu-2399-GPA
- HDU 2399 GPA
- hdu 2399 GPA
- gpa HDU 2399
- hdu 2399 GPA
- GPA
- GPA
- GPA
- GPA
- GPA
- GPA
- GPA
- GPA
- GPA
- GPA
- Lua函数
- u-boot 学习之启动过程完全分析
- select函数详解及应用
- android R文件丢失
- Enjoy everyday(3)——大雨
- 2399:GPA
- 深入理解 指针函数与函数指针
- 用vec类型变量构造Mat(解决一个问题)
- POJ 3468 A Simple Problem with Integers
- poj 3026 Borg Maze 最小生成树+bfs
- cocos2d-x 2.0 动画的创建(利用swf创建动画)
- JavaBean与表单
- MakeFile 详解
- lua调用