• 《算法竞赛入门经典》1.5.6上机练习


    习题1-1 平均数(average)

    1 #include<stdio.h>
    2 int main()
    3 {
    4     int a,b,c;
    5     scanf("%d%d%d",&a,&b,&c);
    6     printf("%0.3lf",(a+b+c)/3.0);
    7     return 0;
    8 }

    本题重点在于格式输出,类型转化……

    不过在

    printf("%0.3lf",(a+b+c)/3);时如输入1 1 2则输出0,这个我还真的搞不清楚了,int型的不是得到1再转double吗……

    习题1-2 温度(temperature)
    1 #include<stdio.h>
    2 int main()
    3 {
    4     float f,c;
    5     scanf("%f",&f);
    6     printf("%0.3f",5*(f-32)/9.0);
    7     return 0;
    8 }
    
    

    这个在

    scanf("%f",&f);将%f改成%d,输入32同样出现错误,结果输出:-17.778.正常不是应该为0吗……虽然有int与float的转化,但是感觉不会啊。。。

    我搜了一下网页http://tieba.baidu.com/p/88194315
    为什么我 
    float a=3; 
    printf("a=%d
    ",a) 
    结果a=0啊? 
    虚心求教
    
    由于你把a定义成folat型了,可是你输出是int型。输出式计算机就自动把float型的值转换成int型,就是把float的后16位赋给int数。而在这题中float数的后16位全是零。所以输出的是0。 
    或者进行强制类型转换。或者用%f型输出��

    感觉这个说的在理。

    习题1-3 连续和(sum)

    1 #include<stdio.h>
    2 int main()
    3 {
    4     int n;
    5     scanf("%d",&n);
    6     printf("%d",n*(n+1)/2);
    7     return 0;
    8 }

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

     1 #include<stdio.h>
     2 #include<math.h>
     3 int main()
     4 {
     5     const float pi=4.0*atan(1.0);
     6     int n;
     7     scanf("%d",&n);
     8     printf("%lf  %lf",sin(n*pi/180),cos(n*pi/180) );
     9     return 0;
    10 }

    关键在于数学……

    习题1-5 距离(distance)

     1 #include<stdio.h>
     2 #include<math.h>
     3 int main()
     4 {
     5     float x1, x2, y1, y2;
     6     float s;
     7     scanf("%f%f%f%f",&x1,&y1,&x2,&y2);
     8     s=sqrt( (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2) );
     9     printf("%f", s);
    10     return 0;
    11 }

    习题1-6 偶数(odd)

     1 #include<stdio.h>
     2 #include<math.h>
     3 int main()
     4 {
     5     int n;
     6     scanf("%d",&n);
     7     if(n%2==0)
     8         printf("Yes
    ");
     9     else
    10         printf("No
    ");
    11     return 0;
    12 }

    习题1-7 打折(discount)

     1 #include<stdio.h>
     2 #include<math.h>
     3 int main()
     4 {
     5     int n;
     6     float price;
     7     scanf("%d",&n);
     8     if(n>=4)
     9     {
    10          price=0.85*n*95;
    11     }
    12 
    13     else
    14     {
    15         price=1.0*n*95;
    16     }
    17         printf("%0.2f
    ",price);
    18     return 0;
    19 }

    习题1-8 绝对值(abs)

     1 #include<stdio.h>
     2 #include<math.h>
     3 int main()
     4 {
     5     float n;
     6     scanf("%f",&n);
     7     printf("%0.2f",fabs(n));
     8     return 0;
     9 }
    10 这个有些问题,会四舍五入……
    11 下面的方法也会
    12 #include<stdio.h>
    13 #include<math.h>
    14 int main()
    15 {
    16     float n;
    17     scanf("%f",&n);
    18     if(n>=0)
    19         printf("%0.2f",n);
    20     else
    21         printf("%0.2f",-n);
    22     return 0;
    23 }

    习题1-9 三角形(triangle)

     1 #include<stdio.h>
     2 #include<math.h>
     3 int main()
     4 {
     5     int a, b, c;
     6     int t;
     7     scanf("%d%d%d",&a,&b,&c);
     8 
     9     //1 找斜边:使用程序1-14三整数排序
    10     if(a>b){t=a;a=b;b=t;}
    11     if(a>c){t=a;a=c;c=a;}
    12     if(b>c){t=b;b=c;c=t;}
    13 
    14     //2 判断能否构成三角形
    15     if( c >= b + a )
    16     {
    17         printf("not a triangle
    ");
    18         return 0;
    19     }
    20 
    21     //3 判断能否构成rt三角形
    22     if( b*b + a*a == c*c )
    23         printf("yes
    ");
    24     else
    25         printf("no
    ");
    26     return 0;
    27 
    28 }

    习题1-10 年份(year)

    判断闰年条件①:非世界年,年数除以4,无余为闰,有余为平;②世纪年,年数除以400,无余为闰有余平。

     1 #include<stdio.h>
     2 #include<math.h>
     3 int main()
     4 {
     5     int year;
     6     scanf("%d",&year);
     7     if(year%100==0)
     8     {
     9         if(year%400==0)
    10             printf("leap year
    ");
    11         else
    12             printf("no leap year
    ");
    13     }
    14     else
    15     {
    16         if(year%4==0)
    17             printf("leap year
    ");
    18         else
    19             printf("no leap year
    ");
    20     }
    21     return 0;
    22 
    23 }
     
     
     
  • 相关阅读:
    beautifulsoup部分知识点
    navicat 结合快捷键
    byte[i] & 0xFF原因(byte为什么要与上0xff?)
    Java parseInt()和parseFloat()的用法
    parse 方法
    getTime()和parse()的区别
    使用SQL Profiler trace(2005)的经验分享(新建跟踪、分析跟踪文件)
    DWZ使用笔记
    js中 json详解
    src与href属性的区别
  • 原文地址:https://www.cnblogs.com/LzKlyhPorter/p/4187892.html
Copyright © 2020-2023  润新知