算法竞赛入门经典第二版第一章习题

chapter01 语言程序设计入门

习题

1.1 平均数(average)

image-20240621194024174

//
// Created by lanys on 2024/6/21.
// 平均数(average)
#include <cstdio>

int main() {

    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);

    // 因为结果是double类型,所以需要先将一个int型的强转为double型
    double avg = ((double) a + b + c) / 3;
    printf("%.3lf", avg);

    return 0;
}
1.2 温度(temperature)

image-20240621195419828

//
// Created by lanys on 2024/6/21.
// 温度(temperature)
#include <cstdio>

int main() {

    double f;
    scanf("%lf", &f);
    double c = 5 * (f - 32) / 9;
    printf("%.3lf", c);

    return 0;
}
1.3 连续和(sum)

image-20240621195519470

//
// Created by lanys on 2024/6/21.
// 连续和(sum)
#include <cstdio>

int main() {

    int n;
    scanf("%d", &n);

    // 前n项和的公式
    int sum = n * (n + 1) / 2;
    printf("%d", sum);


    return 0;
}
1.4 正弦和余弦(sin and cos)

image-20240621195623582

//
// Created by lanys on 2024/6/21.
// 正弦和余弦(sin and cos)
// 这里有一点需要注意的是,如果直接用角度计算,输出的值可能不是你所期望的,所以我先是将角度转为了弧度来计算,结果ok
#include <cstdio>
#include <cmath>

int main() {

    int n;
    scanf("%d", &n);

    // 将角度转为弧度
    double radius = n * M_PI / 180.0;
    double sin_result = sin(radius);
    double cos_result = cos(radius);

    printf("sin = %.4lf, cos = %.4lf", sin_result, cos_result);

    return 0;
}
1.5 打折(discount)

image-20240621195707784

//
// Created by lanys on 2024/6/21.
// 打折(discount)
// 首先输入衣服件数,然后算出money,在判断是否满足300元来决定打折与否
#include <cstdio>

int main() {

    int n;
    scanf("%d", &n);

    double money = 95.0 * n;
    if (money >= 300) money = money * 0.85;
    printf("%.2lf", money);



    return 0;
}
1.6 三角形(triangle)

image-20240621195827186

//
// Created by lanys on 2024/6/21.
// 三角形(triangle)
// 判断三角型是否满足两边之和大于第三边,在判断是否满足直接三角形的条件
#include <cstdio>

int main() {

    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);

    if (a + b > c && a + c > b && b + c > a) {
        if (a * a + b * b == c * c || b * b + c * c == a * a || a * a + c * c == b * b) printf("yes");
        else printf("no");
    } else printf("not a triangle");

    return 0;
}
1.7 年份(year)

image-20240621195906893

//
// Created by lanys on 2024/6/21.
// 年份(year)
// 如果能被4整除并且不能被100整除是闰年,能被400整除是闰年
#include <cstdio>

int main() {

    int year;
    scanf("%d", &year);

    if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) printf("yes");
    else printf("no");


    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值