c语言小练习(蛮好玩的)

1.求三个数的平均数,要求保留三位小数位

#include <conio.h>
#include<stdio.h>
int main(){
    int a,b,c;
    float aver;
    scanf("%d%d%d",&a,&b,&c);
    aver = (a+b+c)/3.0;//整数除以小数,才会变为小数
    printf("%.3lf\n",aver);
    getch();
    return 0;
}


2.根据华氏温度f,获取摄氏温度c,保留3位小数。提示:c=5(f-32)/9。

#include <conio.h>
#include<stdio.h>
int main(){
    float f,c;
    scanf("%f",&f);//获取华氏温度
    c = 5*(f-32)/9.0;
    printf("%.3lf\n",c);//保留3位小数
    getch();
    return 0;
}


3.连续和,输入正整数n,输出1+2+...+n的值。

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


4.判断一个数是否为偶数,如果是,则输出“yes”,否则输出“no”。

#include <conio.h>
#include<stdio.h>
int main(){
    int n;
    bool isodd;
    scanf("%d",&n);
    if(n%2 == 1){
      isodd = false;
    }else{
      isodd = true;
    }
    if(isodd){
      printf("yes");
    }else{
      printf("no");
    }
    getch();
    return 0;
}

 



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

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

 


6.输入三角形三边长度值,判断它是否能为直角三角形的三个边长。
分析:如果两个短边的长度之和小于第三个边,则不是三角形。如果两个短边的平方的和等于第三边平方,则是直角三角形,否则不是。

#include <conio.h>
#include<stdio.h>
int main(){
    int a,b,c,t;
    scanf("%d%d%d",&a,&b,&c);
    if(a>b){t = a;a = b;b = t;}
    if(a>c){t = a;a = c;c = t;}//a是最小的了
    if(b>c){t = b;b = c;c = t;}//b是第二小的了
    if((a+b)<=c){
      printf("not a triangle");
    }else if((a*a + b*b)==c*c){
      printf("yes");
    }else{
      printf("no");
    }
    getch();
    return 0;
}

 


7.年份计算
输入一个年份,判断是否为闰年。
分析:
有两种情况是闰年,一个是可以被4整除同时不被100整除。
二一个是可以被400整除。
其余情况,则不是闰年。
(1900年不是闰年,2000年是闰年)

#include <conio.h>
#include<stdio.h>
int main(){
    int year;
    scanf("%d",&year);
    if((year%4==0&&year%100!=0)||year%400==0){
      printf("yes");
    }else{
      printf("no");
    }
    getch();
    return 0;
}

小结:

1.变量命名清晰

2.思路清晰

3.优化计算

4.分析问题,解决问题

posted @ 2013-07-15 22:45  TBHacker  阅读(682)  评论(0编辑  收藏  举报