算法竞赛入门经典_1.5_习题练习

来源:互联网 发布:unity3d js教程 编辑:程序博客网 时间:2024/04/29 07:05
1.温度问题

 

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

 

运行结果:

2.平均数问题

#include<stdio.h>int main(){    int a, b, c;    scanf("%d%d%d", &a, &b, &c);    double d = (a+b+c)/3.0;    printf("%.3lf\n",d);    return 0;}

//保留三位小数%.3f

运行结果:

3.连续和问题

#include <stdio.h>int main(){    int n=0, sum;    while(n < 1 && scanf("%d", &n));    sum = n*(1+n)/2;    printf("%d\n", sum);    return 0;}

运行结果:

4.正弦和余弦问题

#include <stdio.h>#include <math.h>int main(){    const double pi = acos(-1.0); // pi使用acos(-1.0)求得M_PI不是ANSI C的标准    int n =0;    while( (n <1 || n > 359) && scanf("%d", &n) ); //1-359    double th = n/180.0 * pi; //转换成弧度制    printf("%lf %lf\n", sin(th), cos(th));    return 0;}

运行结果:

5.打折问题

#include <stdio.h>int main(){    int n = 0;    double amount = 0;    while( n < 1 && scanf("%d", &n) );    if(n*95 >= 300)        amount = n*95*0.85;    else        amount = n*95;    printf("%.2lf\n", amount);    return 0;}

运行结果:

6.三角形问题

#include <stdio.h>int main(){    int a = 0, b = 0, c = 0;    while( (a * b *c < 1 && (a<1 || b<1 || c<1)) &&( scanf("%d%d%d", &a, &b, &c ) <= 3))    {        fflush(stdin);    };    if(!(a+b > c && a+c > b && b+c > a))        printf("not a triangle\n");    else    {     if(a*a + b*b == c*c || a*a + c*c == b*b || b*b + c*c == a*a)        printf("yes\n");    else        printf("no\n");    }    return 0;}

运行结果:

7.闰年问题

 

#include <stdio.h>int main(){    int y = 0;    while(y <1 && scanf("%d", &y) <= 1)    {        fflush(stdin);    }    if((y%4 == 0 && y%100!=0 )|| (y % 400 == 0) )    {        printf("yes\n");    }    else    {        printf("no\n");    }    return 0;}

 

//闰年的条件
//4的倍数,但不是100的倍数
//或者是400的倍数

运行结果:

关于scanf控制用户输入问题请看笔者这篇随笔

http://www.cnblogs.com/ncgds/p/7102967.html

 

有时候不逼自己一把怎么知道自己不行?

 

原创粉丝点击