C语言学习日常打卡

巩固选择结构

习题一

T1.某商场进行消费打折活动,消费金额p越高,折扣d越大,其标准如下:

p<200 d=0%

200<=p<400 d=5%

400<=p<600 d=10%

600<=p<1000 d=15%

p>=1000 d=20%

‎ 要求用switch语句编程,输入消费金额,输出打折优惠后的应付金额。

# include <stdio.h>
# include <stdlib.h>
int main()
{
    int p;
    double d, y;
    printf("请输入消费金额:");
    scanf_s("%d", &p);
    switch (p / 200)
    {
    case 0:d = 0;    break;
    case 1:d = 0.05;    break;
    case 2:d = 0.1; break;
    case 3:
    case 4: d = 0.15; break;
    default:d = 0.2; break;
    }
    y = p * (1 - d);
    printf("优惠后应付的金额:%.2lf\n", y);
    system("pause");
    return 0;
}

习题二

T2.给一个不多于3位的正整数,要求:①求出它是几位数;②分别打印出每一位数字;③按逆序打印出各位数字,例如原数为321,应输出123。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
    int t, a, b, c;
    srand(time(NULL));
    t = 1+rand()%1000;
    printf("随机产生的多于3位的正整数:%d\n", t);
    if (t >= 100)
    {
        a = t / 100;
        b = t / 10 % 10;
        c = t % 10;
        printf("三位数:\n百位数:%d\n十位数:%d\n个位数:%d\n", a, b, c);
        printf("%d%d%d\n", c, b, a);
    }
    else if (t < 100 && t >= 10)
    {
        a = t / 10;
        b = t % 10;
        printf("两位数:\n十位数:%d\n个位数:%d\n", a, b);
        printf("%d%d\n", b, a);
    }
    else
    {
        a = t % 10;
        printf("个位数:\n个位数:%d\n", a);
        printf("%d", a);
    }
    system("pause");
    return 0;
}

习题三

T3.四叶玫瑰数是指一个四位数,它的每位上的数字的 4次方之和等于它本身(例如:1^4 + 6^4+ 3^4+ 4^4= 1634)。从键盘输入一个四位数,判断该数是否是四叶玫瑰数。

(四叶玫瑰数有:1634、8208、9474)

‎# include <stdio.h>
# include <stdlib.h>
int main()
{
    int t, a, b, c, d, y;
    printf("输入一个四位整数:");
    scanf_s("%d",&t);
    a = t / 1000;
    b = t / 100 % 10;
    c = t / 10 % 10;
    d = t % 10;
    printf("%d\n%d\n%d\n%d\n",a,b,c,d);
    y = a * a * a * a + b * b * b * b + c * c * c * c + d * d * d * d;
    if (t == y)
        printf("%d是四叶玫瑰数", t);
    else
        printf("%d不是四叶玫瑰数", t);
    system("pause");
    return 0;
}

习题四

T4.

身体质量指数(Body Mass Index,BMI)在国际上常用来衡量人体肥胖程度,BMI 通过人体体重和身高两个数值获得相对客观的参数,并用这个参数所处范围衡量身体质量。

体重指数BMI=体重/身高的平方(国际单位kg/m^2),表1所示为BMI分类及参考标准。

表1 BMI分类及参考标准

BMI 分类

参考标准

偏瘦

<18.5

正常

18.5~24.99

偏胖

25~27.99

肥胖

28~31.99

重度肥胖

≥32

从键盘输入某人的体重(kg)和身高(m),计算他的BMI,并输出其身材胖瘦情况。

#include <stdio.h>
#include <stdlib.h>
int main()
{    
    float m, h,BMI;
    printf("请输入你的体重(kg):");
    scanf_s("%f",&m);
    printf("请输入你的身高(m):");
    scanf_s("%f",&h);
    BMI = m / h / h;
    printf("BMI指数为:%.2f\n",BMI);
    if (BMI < 18.5)
        printf("偏瘦\n");
    else if (BMI >= 18.5&& BMI <= 24.99 )
        printf("正常\n");
    else if (BMI >= 25&& BMI <= 27.99)
        printf("偏胖\nn");
    else if (BMI >= 28&& BMI<=31.99)
        printf("肥胖\n");
    else
        printf("重度肥胖\n");
    system("pause");
    return 0;
}

习题五

‌T5.实现下述分段函数,要求自变量与函数值均为双精度类型。

‌ 当x<7时,f(x)=sin(x);

‌当7<=x<25时,f(x)=6cos(x)/x+3;

‌当x为其他值时,f(x)=(3x^3-x)^(1/3).

‌#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define y    (sin(x))
#define y1    (6*cos(x)/x+3)
int main()
{
    double x, s,y2,t,c;
    c = 1 / 3.0;
    scanf_s("%lf", &x);
    t = pow(x,3);
    t = 3 * t - x;
    y2 = pow(t, c);
    if (x < 7)
    {
        s = y;
        printf("%lf", s);
    }
    else if (x >= 7 && x < 25)
    {
        s = y1;
        printf("%lf", s);
    }
    else
    {
        s = y2;
        printf("%lf", s);
    }
    system("pause");
    return 0;
}

今天你学会了吗?

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

是先森丫

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

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

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

打赏作者

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

抵扣说明:

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

余额充值