《算法竞赛入门经典》第一章习题解答

习题1-1 平均数(average)

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

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a, b, c;
    double avg = 0.0;
    scanf("%d%d%d", &a, &b, &c);
    avg = (a + b + c) / 3.0; //除3.0转化为浮点数
    printf("%.3f", avg);
    system("pause");
    return 0;
}

习题1-2 温度(temperature)

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

#include <stdlib.h>
#include <stdio.h>
int main()
{
    double f, c;
    scanf("%lf", &f);
    c = 5 * (f - 32) / 9.0;
    printf("%.3f", c);
    system("pause");
    return 0;
}

 习题1-3 连续和(sum)

输入正整数n,输出1+2+…+n的值。提示:目标是解决问题,而不是练习编程。

#include <stdlib.h>
#include <stdio.h>
int main()
{
    int n, m;
    scanf("%d", &n);
    m = ((1 + n) * n) / 2;
    printf("%d", m);
    system("pause");
}

习题1-4 正弦和余弦(sin和cos)

输入正整数n(n<360),输出n度的正弦、余弦函数值。提示:使用数学函数。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int n;
    const double pi = acos(-1.0);//利用三角函数求出pi
    scanf("%d", &n);
    double s, c;
    s = sin((n / 180.0) * pi);
    c = (n / 180.0) * pi;
    printf("%f %f", s, cos(c));
    system("pause");
    return 0;
}

习题1-5 打折 (discount)

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

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a;
    scanf("%d", &a);
    double b;
    b = 95.0 * a;
    if (b >= 300.0)
    {
        b = b * 0.85;
    }
    printf("%.2f", b);
    system("pause");
    return 0;
}

习题1-6 三角形(triangle)

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

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    if (a + b > c & a + c > b & a + c > b)
        if (a * a + b * b == c * c || a * a + c * c == b * b || b * b + c * c == a * a)
            printf("yes");
        else
            printf("no");
    else
        printf("not a triangle");
    system("pause");
    return 0;
}

习题1-7 年份(year)

输入年份,判断是否为闰年。如果是,则输出yes,否则输出no。提示:简单地判断除以4的余数是不够的。

//闰年规则 4的倍数但不是100的倍数,或者是400的倍数
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a;
    scanf("%d", &a);
    if ((a % 4 == 0 && a % 100 != 0 || (a % 400 == 0)))
        printf("yes");
    else
        printf("no");
    system("pause");
    return 0;
}

思考题

问题1:int型整数的最小值和最大值是多少(需要精确值)?

最小值是 -2,147,483,648(-2^31);最大值是 2,147,483,647(2^31 - 1;

#include <stdio.h>
#include <stdlib.h>

int main()
{
    for (int i = 2147483000;; i++)
    {
        printf("%d\n", i); //观察输出值,当到达最大值后会从最小值开始输出
    }
    system("pause");
    return 0;
}

问题2:double型浮点数能精确到多少位小数?或者,这个问题本身值得商榷?

具体精度是人为规定的,但是最高到小数点后16位

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("%.19f", 1.0 / 3.0); //观察输出结果,发现只有前16位是正确的
    system("pause");
    return 0;
}

问题3:double型浮点数最大正数值和最小正数值分别是多少(不必特别精确)?

#include <stdio.h>
#include <stdlib.h>
int main()
{
    double i = 0.9999999999999001;
    for (i > 0;; i += 0.0000000000000001) //等待溢出即可
    {
        printf("%.16lf\n%.16lf\n", i, i - 1);
    }
    system("pause");
    return 0;
}

问题4:逻辑运算符号“&&”、“||”和“!”(表示逻辑非)的相对优先级是怎样的?也就是说,a&&b||c应理解成(a&&b)||c还是a&&(b||c),或者随便怎么理解都可以?

相对优先级 || < && < !

#include <stdio.h>
#include <stdlib.h>

int main()
{
    if (0 && 0 || 1) //两种情况一种为0一种为1
        printf("&&>||");
    else
        printf("&&<||");
    if (!1 || 1)
        printf("!>||");
    else
        printf("!<||");
    if (!1 && 0)
        printf("&&>!");
    else
        printf("!>&&");
    system("pause");
    return 0;
}

问题5:if(a)if(b)x++;else y++的确切含义是什么?这个else应和哪个if配套?有没有办法明确表达出配套方法?

else就近原则

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a = 0;
    int b = 0;
    //匹配第一个if,ab都不变,匹配第二个,b++
    if (1)
        if (0)
            a++;
        else
            b++;
    printf("%d %d", a, b);
    system("pause");
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值