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
    关注
    评论

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?