if与switch选择结构的习题精讲

目录

导读:

1.有三个整数a,b,c,由键盘输入,输出其中最大数

方法一:用多个if语句判断

方法二:三目运算符

2.输出一个数的平方根

3.函数y与x表达式,输入x值,输出对应y的值 

4.按学生成绩评成绩等级 

方法一:if判断

方法二:switch

5.给出一个不多于5位的正整数,要求:

(1)求出它是几位数

(2)分别输出每一位数字

(3)按逆序输出各位数字

3个要求总体代码

导读:

该知识点讲解在:

分支语句(选择结构):if、switch_流浪者与猴的博客-CSDN博客

1.有三个整数a,b,c,由键盘输入,输出其中最大数

方法一:用多个if语句判断

int main()
{
    int a = 0;
    int b = 0;
    int c = 0;
    scanf("%d%d%d",&a,&b,&c);
    int max = a;
    if (a<b)
    {
        if (b < c)
        {
            max = c;
        }
        else
        {
            max = b;
        }
    }
    else if (a<c)
    {
        max = c;
    } 
    
    printf("%d\n",max);
    return 0;
}

思路: 

定义一个max,先把a赋给max,判断a和b的大小,如果a<b则进入{ },再判断b和c的大小进行给max的赋值,如果进入if,就不会再次进入else中

方法二:三目运算符

int main()
{
    int a = 0;
    int b = 0;
    int c = 0;
    scanf("%d%d%d", &a, &b, &c);
    int max = a > b ? a : b;
    max = max > c ? max : c;
    printf("%d\n", max);
	return 0;
}

思路: 

条件表达式:如果判断a是否大于b,如果是返回a,反之返还b

接着再次判断max与c的大小

2.输出一个数的平方根

从键盘输人一个小于1000的正数,要求输出它的平方根(如平方根不是整数,则输出其整数部分)。要求在输人数据后先对其进行检查是否为小于1000 的正数。若不是,则要求重新输入

#include <stdio.h>
#include <math.h>
int main()
{
    double a = 0;
    double b = 0;
    scanf("%lf", &a);
    b = sqrt(a);
    while (a >= 1000)
    {
        printf("请重新输入\n");
        scanf("%lf", &a);
    }
    printf("%4.0lf", b);

    return 0;
}

思路: 

用while循环来实现“输入大于1000的数重新输入”的需求

3.函数y与x表达式,输入x值,输出对应y的值 

int main()
{
    int x = 0;
    int y = 0;
    scanf("%d",&x);
    if (x < 1)
    {
        y = x;
    }
    else if (x >= 1 && x < 10)
    {
        y = 2*x - 1;
    }
    else if (x >= 10)
    {
        y = 3 * x - 11;
    }
    printf("%d\n",y);
    return 0;

}

思路:

用多个if判断语句来实现,注意要使用else if而不是三个连续的if
连续用多个if,每个if语句都会判断一次,增加运行时间,甚至有时结结果会出错

4.按学生成绩评成绩等级 

给出一百分制成绩,要求输出成绩等级'A'、'B'、'C'、'D'、'E'。90分以上为'A',80~89分为'B',70~79分为'C',60~69分为'D',60分以下为'E'

方法一:if判断

简单的用 if 来判断成绩的区间

int main()
{
    double score = 0.0;
    scanf("%lf",&score);
    if (score >= 90)
    {
        printf("A");
    }
    else if (score >= 80 && score <= 89)
    {
        printf("B");
    }
    else if (score >= 70 && score <= 79)
    {
        printf("C");
    }
    else if (score >= 60 && score <= 69)
    {
        printf("D");
    }
    else if (score < 60)
    {
        printf("E");
    }
    return 0;
}

方法二:switch

int main()
{
	double score = 0.0;
	char grade = 0;
	scanf("%lf", &score); 
	switch ((int)(score/10))
	{
	case 10:
	case 9:
		grade = 'A';
		break;
	case 8:
		grade = 'B';
		break;
	case 7:
		grade = 'C';
		break;
	case 6:
		grade = 'D';
		break;
	case 5:		
	case 4:
	case 3:
	case 2:
	case 1:
	case 0:
		grade = 'E';
		break;
	}
	printf("%c\n", grade);
	return 0;
}

因为60分以下全为 E ,所以case可以不用break停止,只在最后加上break即可 

5.给出一个不多于5位的正整数,要求:

(1)求出它是几位数

int main()
{
	int n = 0;
	printf("请输入一个不多于5位数的正整数:");
	scanf("%d", &n);
	int count = 0;
	while (n)
	{
		n = n / 10;
		count++;
	}
	printf("该数是%d位数\n", count);
	return 0;
}

 思路:

进入while循环,用count计数,每一次进入循环count自增1,n等于n/10

(2)分别输出每一位数字

int main()
{
	int n = 0;
	printf("请输入一个不多于5位数的正整数:");
	scanf("%d", &n);
	int count = 0;
	int ten = n;
	while (ten)
	{
		ten = ten / 10;
		count++;
	}
	printf("该数是%d位数\n", count);
	printf("每位数字为:");
	int i = 0;
	for (i = count; i > 0; i--)
	{
		int j = 0;
		ten = n;
		for (j = 1; j < i; j++)
		{
			ten = ten / 10;
		}
		printf("%d ", ten % 10);
	}
	return 0;
}

思路: 

用两个for循环来实现,这里另定义一个变量,把n的值赋给它,避免n在计算过程中丢失原本数据

(3)按逆序输出各位数字

int main()
{
	int n = 0;
	printf("请输入一个不多于5位数的正整数:");
	scanf("%d", &n);
	int count = 0;
	int ten = n;
	while (ten)
	{
		printf("%d", n % 10);
		n /= 10;
		count++;
	}
	printf("该数是%d位数\n", count);
	return 0;
}

3个要求总体代码

int main()
{
	int n = 0;
	printf("请输入一个不多于5位数的正整数:");
	scanf("%d", &n);
	int count = 0;
	int ten = n;
	while (ten)
	{
		ten = ten / 10;
		count++;
	}
	printf("该数是%d位数\n", count);
	printf("每位数字为:");
	int i = 0;
	for (i = count; i > 0; i--)
	{
		int j = 0;
		ten = n;
		for (j = 1; j < i; j++)
		{
			ten = ten / 10;
		}
		printf("%d ", ten % 10);
	}
	printf("\n");
	printf("逆序数字为:");
	while (count)
	{
		printf("%d", n % 10);
		n /= 10;
		count--;
	}
	return 0;
}
  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流浪者与猴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值