zzpzm 2017-02-16 02:57 采纳率: 0%
浏览 1186
已结题

杭电oj上GPA超时的问题

代码提交上去之后说是超时,可我这种写法我不太明白怎么把时间复杂度降低。下面是题目以及我写的代码,希望大家能帮忙解决下,谢谢了。
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

代码:
#include
#include

int main()
{
float sum;
float count;
char ch;
int flag;
while(1)
{
sum=0;
count=0;
flag=1;
while((ch=getchar())!='\n')
{
switch(ch)
{
case'A':sum=sum+4;count++;break;
case'B':sum+=3;count++;break;
case'C':sum+=2;count++;break;
case'D':sum+=1;count++;break;
case'F':sum+=0;count++;break;
case'E':flag=0;break;
case' ':break;
}
}
if(flag)
{
printf("%.2f\n",sum/count);
}
else
{
printf("Unknown letter grade in input\n");

    }

}
return 0;

}

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-02-16 03:11
    关注
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?