您的位置:首页 > 编程语言

《算法竞赛入门经典》习题参考代码(第一章)

2015-03-23 10:11 253 查看
//习题1-1 平均数
4000

//author by liuchao

//2015-3-17

#include<stdio.h>

int main()

{

    int a, b, c, sum;

    double avg;

    

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

    

    sum = a + b + c;

    avg = sum / 3.0;

    

    printf("%.3lf\n",avg);

    

    return 0;
}

//习题1-2 温度

//author by liuchao

//2015-3-17

#include<stdio.h>

int main()

{

    int f;

    double c;

    scanf("%d",&f);

    c = 5 * (f - 32) / 9.0;

    printf("%.3lf\n",c);

    return 0;

}

//习题1-3 连续和 

//author by liuchao

//2015-3-17

#include<stdio.h>

int main()

{

    int n;

    scanf("%d",&n);

    printf("%d\n",n * (n + 1) / 2);

    return 0;

}

//习题1-4 正弦和余弦 

//author by liuchao

//2015-3-17

//360度 = 2PI弧度 

#include<stdio.h>

#include<math.h>

int main()

{

    const double PI = 4.0 * atan(1.0);

    int n;

    scanf("%d",&n);

    printf("%llf %llf\n",sin(PI / 180 * n),cos(PI / 180 * n));

    return 0;



//习题1-5 打折 

//author by liuchao

//2015-3-17

#include<stdio.h>

int main()

{

   int n;

   double sum;

   scanf("%d",&n);

   sum = n * 95;

   if (sum >= 300)

      sum = sum * 0.85;

   printf("%.2lf\n",sum);

   return 0;  

}

//习题1-6 三角形

//author by liuchao

//2015-3-17

#include<stdio.h>

int main()

{

   int a, b, c;

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

   if (( a + b > c) && (b + c > a) && (c + a > b))

   {

        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 闰年 

//author by liuchao

//2015-3-17

#include<stdio.h>

int main()

{

    int n;

    scanf("%d",&n);

    if (((n % 4 == 0) && (n % 100 != 0)) || (n % 400 == 0))

       printf("yes");

    else

       printf("no");

    

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: