寻光


私信TA

用户名:RITD

访问量:22636

签 名:

追寻最优解

等  级
排  名 546
经  验 4306
参赛次数 0
文章发表 13
年  龄 20
在职情况 学生
学  校 East Electricity
专  业 计科

  自我简介:

很菜所以很努力

解题思路:    1. 先看题目,找出适合的方法,比如这的题目就和条件有关(分数和评级有关),因此可以使用if条件语句,if else嵌套,Switch语句来做此题.

                  2. 先声明一个int类型的变量,来获取计算机给这个变量分配内存.

                  3. 利用scanf()获取用户的输入.

                  4. 利用自己所选语句来进行判定并输出.

注意事项:    因为题目要求是输入0到100,故我们不需要用代码说明用户输入为负值、大于100、带小数的情况。

                  if 条件语句后面,注意别加分号

                  

参考代码:

i:只利用if语句

#include <stdio.h>
int main(void)
{
	int score;
	
	scanf("%d", &score);
	
	if (score >= 90)
	   printf("A");
	if (score >= 80 && score < 90)
	   printf("B");
	if (score >= 70 && score < 80)
	   printf("C");
	if (score >= 60 && score < 70)
	   printf("D");
	if (score < 60)
	   printf("E");
	   
	return 0;
}

ii:if else嵌套

#include <stdio.h>
int main(void)
{
	int score;
	
	scanf("%d", &score);
	
	if (score >= 90)
	   printf("A");
	else
	    if (score >= 80)
	        printf("B");
	    else
	        if (score >= 70)
	           printf("C");
	        else
	            if (score >= 60)
	               printf("D");
	            else 
	                printf("E");
	    
	return 0;
}

3.switch语句

#include<stdio.h>
int main(){
    int score;
    scanf("%d",&score); 
    switch(score/10){            // a/10是取整,说明要执行的语句
        case 10:             // 执行完这条语句,没有break,因此会继续向下执行语句9
        case 9:     putchar('A');
                    break;
        case 8:     putchar('B');
                    break;
        case 7:     putchar('C');
                    break;
        case 6:     putchar('D');
                    break;
        default:      putchar('E');      
    }
    
    return 0;
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区