HDU 2399 GPA(简单字符串的处理)

来源:互联网 发布:淘宝佣金软件购买 编辑:程序博客网 时间:2024/04/26 23:27

GPA

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2338    Accepted Submission(s): 1379


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
 

Author
2006Rocky Mountain Warmup
 

Source
HDU “Valentines Day” Open Programming Contest 2009-02-14
 

Recommend

题目很简单,就是读取一行字符串,然后再把它转换成相对应的数字,求和之后再求平均。
一开始是这样写的:
while(~scanf("%c",a[0]))
{
i=1;
getchar();
while(~scanf("%c%c",a[i++],c))
{
if(c=='\n')
break;
}
........
}
注意这是错误的代码,这样对于测试数据是对的,但是如果输入了多个空格就不对了,这样对于只有一个空格是对的。
所以我用了string类里面的getline.(或者使用gets函数也可)这样读取一串字符,包括空格也读了进去。最后在吧空格处理一下就好了。
几种读取字符串的函数:
geline:
getline()函数会生成一个包含一串从输入流读入的字符的字符串,直到以下情况发生会导致生成的此字符串结束。1)到文件结束,2)遇到函数的定界符,3)输入达到最大限度。

返回值

成功:返回读取的字节数。
失败:返回-1。
参数:
lineptr:指向存放该行字符的指针,如果是NULL,则有系统帮助malloc,请在使用完成后free释放。
n:如果是由系统malloc的指针,请填0
stream:文件描述符
gets:
从stdin流中读取字符串,直至接受到换行符或EOF时停止,并将读取的结果存放在buffer指针所指向的字符数组中。换行符不作为读取串的内容,读取的换行符被转换为NULL值,并由此来结束字符串。
以上两种均可读取空格。
getchar()读取一个字符;
cin.getline():
此函数一次读取多个字符(包括空白字符),直到读满N-1个,或者遇到指定的结束符为止(默认的是以'\n'结束)。其语法为:
cin.getline(字符指针(char*),字符个数N(int),结束符(char));

AC代码:
#include <iostream>#include<stdio.h>#include<memory.h>#include<string.h>#include<string>using namespace std;//char a[100000];string str,a;int main(){    char c;    int i,j,flag,k;    double s;    while(getline(cin,a))    {        i=a.length();        flag=0;        s=0;        k=0;        for(j=0; j<i; j++)        {            if(a[j]!='A'&&a[j]!='B'&&a[j]!='C'&&a[j]!='D'&&a[j]!='F'&&a[j]!=32)            {                cout<<"Unknown letter grade in input"<<endl;                flag=1;                break;            }            else            {                switch(a[j])                {                case 'A':                    s=s+4;                    break;                case 'B':                    s=s+3;                    break;                case 'C':                    s=s+2;                    break;                case 'D':                    s=s+1;                    break;                    case 32:k++;break;                }            }        }        if(!flag)            printf("%.2lf\n",s/(i-k));        //memset(a,'\0',sizeof(a));    }    return 0;}


0 0
原创粉丝点击