zoukankan      html  css  js  c++  java
  • 算法竞赛入门经典第二版——第一章习题

    习题1-1 平均数

    输入3个整数,输出它们的平均数,保留3为小数

    代码:

    #include <stdio.h>
    
    int main()
    {
        double a, b, c;
        scanf("%lf%lf%lf", &a, &b, &c);
        printf("%.3f", (a + b + c) / 3);
    }
    

    习题1-2 温度

    输入华氏温度f,输出对应的摄氏温度c,保留3位小数。提示:c=5(f-32)/9

    代码:

    #include <stdio.h>
    
    int main()
    {
        double f;
        scanf("%lf", &f);
        printf("%.3f", 5 * (f - 32) / 9);
    }
    

    习题1-3 连续和

    输入正整数n,输出1+2+···+n的值

    代码:

    #include <stdio.h>
    
    int main()
    {
        int n, sum;
        scanf("%d", &n);
        for (int i = 1; i <= n; i++)
        {
            sum += i;
        }
        printf("%d", sum);
    }
    

    习题1-4 正弦和余弦

    输入正整数n(n<360),输出n度的正弦,余弦函数值

    代码:

    #include <stdio.h>
    #include <math.h>
    #define PI 3.1415
    
    int main()
    {
        int n; //n为角度
        scanf("%d", &n);
        printf("%.3lf %.3lf", sin(n * PI / 180), cos(n * PI / 180));
    }
    

    习题1-5 打折

    一件衣服95元,若消费满300元,可打八五折,输入购买衣服件数,输出需要支付的金额(单位:元),保留两位小数。

    代码:

    #include <stdio.h>
    
    int main()
    {
        int n, price = 95;
        double cost;
        scanf("%d", &n);
        cost = n * price;
        if (cost >= 300)
        {
            cost = cost * 0.85;
        }
        printf("%.2lf", cost);
    }
    

    习题1-6 三角形

    输入三角形3条边的长度值(均为正整数),判断是否能为直角三角形的3个边长。如果可以,则输出yes,如果不能,则输出no。如果根本无法构成三角形,则输出not a triangle

    代码:

    #include <stdio.h>
    
    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) || (c * c + a * a == b * b))
            {
                printf("yes
    ");
            }
            else
            {
                printf("no
    ");
            }
        }
        else
        {
            printf("not a triangle
    ");
        }
    }
    

    习题1-7 年份

    输入年份,判断是否为闰年。如果是,则输出yes,否则输出no

    代码:

    #include <stdio.h>
    
    int main()
    {
        int year;
        scanf("%d", &year);
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
        {
            printf("yes
    ");
        }
        else
        {
            printf("no
    ");
        }
    }
    
  • 相关阅读:
    2013 ACM/ICPC Asia Regional Changsha Online G Goldbach
    【转】海量数据求中位数
    【转】如何在vmware中如何设置ip
    十大基本功之testbench
    图像处理与计算机视觉:基础,经典以及最近发展
    Quartus II调用modelsim无缝仿真
    ise和quartus共用仿真软件
    modesim 仿真问题
    Altium Designer设计PCB中如何开槽
    原理图间的 总线接口
  • 原文地址:https://www.cnblogs.com/techoc/p/13489738.html
Copyright © 2011-2022 走看看