7-1 成绩转换 (15分)

#include<stdio.h>
int main()
{
    int s,g;
    scanf("%d",&s);
    if(s>=90)
        g='A';
    else if(s>=80)
        g='B';
    else if(s>=70)
        g='C';
    else if(s>=60)
        g='D';
    else
        g='E';
    printf("%c",g);
}
### 成绩转换算法实现解析 在处理成绩转换问题时,通常需要将输入的百成绩转换为对应的等级制表示。这个问题的实现方式可以有多种,例如使用条件判断语句(`if-else`)或使用 `switch-case` 结构。 #### 使用 `if-else` 实现 通过判断输入的数范围,依次输出对应的等级。例如,输入 `90` 输出 `A`,输入 `85` 输出 `B`。代码结构如下: ```c #include <stdio.h> int main() { int score; scanf("%d", &score); if (score >= 90) { printf("A"); } else if (score < 90 && score >= 80) { printf("B"); } else if (score < 80 && score >= 70) { printf("C"); } else if (score < 70 && score >= 60) { printf("D"); } else { printf("E"); } return 0; } ``` 该方法逻辑清晰,适用于较小范围的条件判断,且易于理解[^1]。 #### 使用 `switch-case` 实现 通过将输入数除以10取整,简化对成绩范围的判断,并使用 `switch` 语句匹配不同的等级。例如: ```c #include <stdio.h> int main() { int result; scanf("%d", &result); switch (result / 10) { case 10: case 9: printf("A"); break; case 8: printf("B"); break; case 7: printf("C"); break; case 6: printf("D"); break; default: printf("E"); break; } return 0; } ``` 这种方法减少了冗长的条件判断,提高了代码的可读性和简洁性[^4]。 #### 使用浮点数输入的 `switch-case` 实现 如果输入的数为浮点数,可以先将其强制转换为整数,再进行范围判断。代码如下: ```c #include <stdio.h> int main(void) { float m; scanf("%f", &m); switch (((int)m) / 10) { case 0: case 1: case 2: case 3: case 4: case 5: printf("E"); break; case 6: printf("D"); break; case 7: printf("C"); break; case 8: printf("B"); break; case 9: case 10: printf("A"); break; } return 0; } ``` 该方法能够处理浮点数输入,同时保持 `switch-case` 的简洁性[^3]。 ### 总结 不同的实现方式各有优劣,`if-else` 更适合初学者理解,而 `switch-case` 则适用于需要简洁代码的场景。在实际应用中,应根据输入数据的特性选择合适的实现方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值