【选择结构】习题

沈阳航空航天大学习题的整理,仅供参考。

1.一个简单计算器

【问题描述】一个简单计算器,根据用户输入的操作数和操作类型进行算术运算,最后打印计算结果。

【输入形式】2*2

【输出形式】4.000000

【评分标准】程序每个注释found下面有一个错误,共2处错误,其他内容保持不变,使之功能符合要求。

#include <iostream>
#include<stdio.h>
int main(void)
{
    float x, y, z = 0;
    /*************found************/
    char n;
    scanf("%f%c%f", &x, &n, &y);
    /*************found************/
    switch (n)
    {
    case '+':  z = x + y;  break;
    case '-':  z = x - y;  break;
    case '*':  z = x * y;  break;
    case '/':  z = x / y;  break;
    }
    printf("%f", z);
    return 0;
}

2.判断一个整数的正负

【问题描述】判断一个整数的正负。输入1个整数。如果输入的为正,则输出1;如果输入的为负则输出-1;如果输入的为0,则输出0。

【输入形式】1个整数

【输出形式】1个整数

【样例输入】-5

【样例输出】-1

#include <iostream>
#include <stdio.h>
int main()
{
    int a, b;
    scanf("%d", &a);
    if (a < 0)
    {
        b = -1;
    }
    else if (a == 0)
    {
        b = 0;
    }
    else
    {
        b = 1;
    }
    printf("%d", b);
	return 0;
}

3.判断一个整数能否被3,7整除

【问题描述】编一个程序,输入一个整数 n,判断它能否被3,7整除;

若能同时被3,7整除则输出 n is divisible by 3 and 7;

能被其中一个数整除输出 n is divisible by 3 or 7;

不能被3,7整除输出 n is not divisible by 3 or 7。

【输入形式】输入正整数 n

【输出形式】 输出提示信息。

【样例输入】2

【样例输出】2 is not divisible by 3 or 7

【样例输入】7

【样例输出】7 is divisible by 3 or 7

【样例输入】105

【样例输出】105 is divisible by 3 and 7

#include<stdio.h>
int main()
{
	int a;
	scanf("%d",& a);
	if (a % 3 == 0)
	{
		if (a % 7 == 0)
			printf("%d is divisible by 3 and 7", a);
		else
			printf("%d is divisible by 3 or 7",a);
	}
	else
	{
		if (a % 7 == 0)
			printf("%d is divisible by 3 or 7",a);
		else
			printf("%d is not divisible by 3 or 7", a);
	}
    return 0;
}

4.判断水仙花数

【问题描述】判断一个三位正整数是否是水仙花数。水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身,例如153。

【输入形式】输入一个三位正整数 n

【输出形式】输出 n is narcissistic number 或 n is not narcissistic number

【样例输入】153

【样例输出】153 is narcissistic number

【样例输入】100

【样例输出】100 is not narcissistic number

#include<stdio.h>
int main()
{
	int a, b, c, n; 
	scanf("%d", & n);
	a = n / 100;
	b = n % 100 / 10;
	c = n % 10;
	if (n == a * a * a + b * b * b + c * c * c)
		printf("%d is narcissistic number",n); 
	else
		printf("%d is not narcissistic number",n);
    return 0;
}

5.判断闰年

【问题描述】写一程序,从键盘上输入年份year(大于400),判断其是否闰年。闰年的条件是:能被4整除、但不能被100整除,或者能被400整除。

【输入形式】输入一个大于400的正整数,以回车结束。

【输出形式】输出判断结果。

【样例输入】2000

【样例输出】2000 is

【样例输入】2015

【样例输出】2015 not

#include<stdio.h>
int main()
{
	int year;
	scanf("%d", &year);
	if ((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0))
		printf("%d is", year);
	else
		printf("%d not", year);
	return 0;
}

6.将输入的小写英文字母修改为大写英文字母

【问题描述】输入1个字符,如果是小写英文字母,则将其修改为大写英文字母,并输出该大写字母;如果是其它字符,则保持不变,原样输出。

【输入形式】1个字符

【输出形式】1个字符

【样例输入】e

【样例输出】E

【样例输入】6

【样例输出】6

#include <iostream>
#include <stdio.h>
int main()
{
    char w;
    scanf("%c", &w);
    if (w >= 'a' && w <= 'z')
        w = w - 32;
    printf("%c", w);
	return 0;
}

7.找出能够组成的最大三位数

【问题描述】输入三个正整数,这三个整数数值范围在0到9之间,且互不相同,请找出由这三个整数组成的最大三位整数。

【输入形式】输入三个整数,不考虑输入错误的情况,数据之间用单个空格隔开。

【输出形式】输出组成的最大三位整数。

【样例输入】1 2 3

【样例输出】321

#include<stdio.h>
int main()
{
	int a, b, c, t;
	scanf("%d%d%d", &a, &b, &c);
	if (a < b)
	{
		t = a;
		a = b;
		b = t;
	}
	if (b < c)
	{
		t = b;
		b = c;
		c = t;
	}
	if (a < b)
	{
		t = a;
		a = b;
		b = t;
	}
	int max = a * 100 + b * 10 + c;
	printf("%d", max);
	return 0;
}

8.根据年和月的值输出该月的天数

【问题描述】从键盘输入年和月的值,然后输出该月的天数。

【输入形式】<年值><空格><月值>

【输出形式】<天数值>

【样例输入】2012 2

【样例输出】 February 2012 has 29 days

【样例输入】2011 7

【样例输出】July 2011 has 31 days

#include<stdio.h>
int main()
{
    int year, month;
    scanf("%d %d", &year, &month);
    switch (month)
    {
    case 2:if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
        printf("February %d has 29 days\n",year);
          else
        printf("February %d has 28 days\n",year);
        break;
    case 1:printf("January %d has 31 days\n",year); break;
    case 3:printf("March %d has 31 days\n",year); break;
    case 5:printf("May %d has 31 days\n", year); break;
    case 7:printf("July %d has 31 days\n", year); break;
    case 8:printf("August %d has 31 days\n", year); break;
    case 10:printf("October %d has 31 days\n", year); break;
    case 12:printf("December %d has 31 days\n", year); break;
    case 4:printf("April %d has 30 days\n", year); break;
    case 6:printf("June %d has 30 days\n", year); break;
    case 9:printf("September %d has 30 days\n", year); break;
    case 11:printf("November %d has 30 days\n", year); break;
    }
	return 0;
}

9.比较三个数的大小,并计算三个数的和、积和平均值

【问题描述】编一个程序,从键盘中输入三个整数 a, b, c;比较三个数的大小,然后输出其中最大的数、最小的数、三个数的和、积和平均值。

【输入形式】输入三个整数 a b c

【输出形式】 输出最大数,最小数,三个数的和,三个数的积,平均值。

【样例输入】1 4 2

【样例输出】4 1 7 8 2.33

【样例说明】平均值保留小数点后2位。

#include<stdio.h>
int main()
{
	int a, b, c, t, num, ji;
    float ave;
	scanf("%d%d%d", &a, &b, &c);
	if (a < b)
	{
		t = a;
		a = b;
		b = t;
	}
	if (b < c)
	{
		t = b;
		b = c;
		c = t;
	}
	if (a < b)
	{
		t = a;
		a = b;
		b = t;
	}
    num = a + b + c;
    ji = a * b * c;
    ave = ( a + b + c ) / 3.0;
	printf("%d %d %d %d %.2f", a, c, num, ji, ave);
    return 0;
}

10.求圆柱体体积

【问题描述】输入圆柱体的底半径及高度,求体积,体积公式为3.14 * r * r * h。

【输入形式】从键盘输入圆柱体底的半径和高度,以回车结束。

【输出形式】圆柱体体积

【样例输入】2.6 3.21

【样例输出】68.14

【样例说明】输出结果保留两位小数

#include<stdio.h>
int main()
{
	double r, h, t = 0;
	scanf("%lf%lf", &r, &h);
	t = 3.14 * r * r * h;
	printf("%.2f", t);
	return 0;
}

11.输入一个五分制的考试成绩输出对应的百分制分数段

【问题描述】输入一个五分制的考试成绩(大写字母A~E),输出对应的百分制分数段。

A对应90~100,

B对应80~89,

C对应70~79,

D对应60~69,

E对应<60,

其它对应error。

【输入形式】1个字符

【输出形式】1个字符串

【样例输入】D

【样例输出】60~69

#include <iostream>
#include <stdio.h>
int main()
{
    char grade;
    scanf("%c", &grade);
    switch (grade)
    {
    case 'A':printf("90~100"); break;
    case 'B':printf("80~89"); break;
    case 'C':printf("70~79"); break;
    case 'D':printf("60~69"); break;
    case 'E':printf("<60"); break;
    default:printf("error");
    }
	return 0;
}

  • 24
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

lnk_

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

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

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

打赏作者

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

抵扣说明:

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

余额充值