《算法竞赛入门经典》第1章习题总结

习题1-1 平均数(average)

<span style="font-size:24px;"><strong><span style="font-size:18px;"><span style="font-size:18px;">#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a, b, c;
    float n;
    scanf("%d%d%d", &a, &b, &c);
    n = (a + b + c) / 3.0;
    printf("%.3f\n", n);
    return 0;
}</span></span></strong></span>

习题1-2 温度(temperature)

<span style="font-size:24px;"><strong><strong><span style="font-size:18px;"><span style="font-size:18px;">#include <stdio.h>

int main()
{
   float f, c;

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

   return 0;
}
</span></span></strong></strong></span>

习题1-3 连续和(sum)

<strong><span style="font-size:24px;"><span style="font-size:18px;"><strong>#include <stdio.h>
#include <stdlib.h>

int main()
{
   int n;
   scanf("%d\n", &n);
   printf("%d\n", n * (n + 1) / 2);

   return 0;
}</strong></span></span></strong>

习题1-4 正弦和余弦(sincos)

<span style="font-size:24px;"><strong><strong><span style="font-size:18px;">#include <stdio.h>
#include <math.h>

int main()
{
    int n;
    const double pi = 4.0 * atan(1.0);
    scanf("%d\n", &n);
    printf("%lf\n%lf\n", sin((pi * n) / 180), cos((pi * n) / 180));
    return 0;
}</span></strong></strong></span>

习题1-5 距离(distance)

<strong><span style="font-size:24px;"><strong><span style="font-size:18px;">#include <stdio.h>
#include <math.h>

int main()
{
    float x1, y1, x2, y2;
    scanf("%f\n%f\n%f\n%f", &x1, &y1, &x2, &y2);
    printf("%lf\n", sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)));
    return 0;
}</span></strong></span></strong>

习题1-6 偶数(odd)

<strong><span style="font-size:24px;"><span style="font-size:18px;"><strong>#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    scanf("%d\n", &n);
    if(n % 2 == 0)
    {
        printf("yes\n");
    }
    else
    {
        printf("no\n");
    }
    return 0;
}</strong></span></span></strong>

习题1-7 打折(discount)

<span style="font-size:24px;"><strong><span style="font-size:18px;"><strong>#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    scanf("%d\n", &n);
    if(n <= 3)
    {
        printf("%d\n", n * 95);
    }
    else
    {
        printf("%.2f\n", n * 95 * 0.85);
    }
    return 0;
}</strong></span></strong></span>

习题1-8 绝对值(abs)

<strong><span style="font-size:24px;"><strong><span style="font-size:18px;">#include <stdio.h>
#include <stdlib.h>

int main()
{
    double n;
    scanf("%lf\n", &n);
    printf("%.2lf\n", fabs(n));
    return 0;
}</span></strong></span></strong>

习题1-9 三角形(triangle)

<strong><span style="font-size:24px;"><span style="font-size:18px;"><strong>#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    if((a==b) && (b==c))
    {
        printf("no");
    }
    else if((a*a+b*b == c*c) || (a*a+c*c == b*b) || (b*b+c*c == a*a))
    {
        printf("yes");
    }
    else
    {
        printf("no");
    }
    return 0;
}</strong></span></span></strong>

习题1-10 年份(year)

<strong><span style="font-size:24px;"><span style="font-size:18px;"><strong>#include <stdio.h>
#include <stdlib.h>

int main()
{
    int n;
    scanf("%d\n", &n);
    if(n % 400 == 0)
    {
        printf("yes");
    }
    else if(n % 4 == 0)
    {
        if(n % 100 != 0)
        {
            printf("yes");
        }
        else
        {
            printf("no");
        }
    }
    else
    {
        printf("no");
    }
    return 0;
}</strong></span></span></strong>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值