紫书《算法竞赛入门经典》课后习题——第1章 程序设计入门

习题1-1 平均数

#include<cstdio>

using namespace std;

int main(){
    float a, b, c;
    scanf("%f%f%f", &a, &b, &c);
    printf("%.3f\n", (a+b+c)/3);
    return 0;
}

习题1-2 温度

#include<cstdio>

using namespace std;

int main(){
    float f;
    scanf("%f", &f);
    printf("%.3f\n", 5*(f-32)/9);
    return 0;
}

习题1-3 连续和

#include<iostream>

using namespace std;

int main(){
    int n, sum=0;
    cin >> n;
    while(n>0){
        sum += n;
        --n;
    }
    cout << sum << endl;
    return 0;
}

习题1-4 正弦和余弦

#include<iostream>
#include<cmath>

#define PI 3.1415926

using namespace std;

int main(){
    int n;
    cin >> n;
    float temp = n*PI/180;  //角度转弧度
    cout << sin(temp) << " " << cos(temp) << endl;
    return 0;
}

习题1-5 打折

#include<cstdio>

using namespace std;

int main(){
    int n;
    scanf("%d", &n);
    float temp = n * 95;
    if(temp < 300){
        printf("%.2f\n", temp);
    }
    else{
        printf("%.2f\n", temp * 0.85);
    }
    return 0;
}

习题1-6 三角形

#include<iostream>
#include<cmath>
#include<algorithm>

using namespace std;

int main(){
    int arr[3];
    cin >> arr[0] >> arr[1] >> arr[2];
    sort(arr, arr+3);  //从小到大排序
    if((arr[0] + arr[1]) > arr[2]){  //任意两边之和大于第三边
        if((arr[0] -arr[1] < arr[2])){  //任意两边之差小于第三边
            if( (pow(arr[0], 2)+pow(arr[1],2)) == pow(arr[2], 2)){  //两只角边的平方和等于斜边的平方
                cout << "yes" << endl;
            }
            else{
                cout << "no" << endl;
            }
        }
        else{
            cout << "not a triangle" << endl;
        }
    }
    else{
        cout << "not a triangle" << endl;
    }
    return 0;
}

习题1-7 年份

#include<iostream>

using namespace std;

int main(){
    int n;
    cin >> n;
    if((n%4 == 0) && (n%100 != 0)){
        cout << "yes" << endl;
    }
    else if(n%400 == 0){
        cout << "yes" << endl;
    }
    else{
        cout << "no" << endl;
    }
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Guan_qiqi

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

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

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

打赏作者

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

抵扣说明:

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

余额充值