假期编程

1.求绝对值(3min)
Problem Description
求实数的绝对值。
Input
输入数据有多组,每组占一行,每行包含一个实数。
Output
对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。
Sample Input
123
-234.00
Sample Output
123.00
234.00
代码如下:
#include<stdio.h>
#include<math.h>
int main(void)
{
    double n;
    while(~scanf("%lf",&n))
    {
        if(n<0)
            n=-n;
        printf("%0.2lf
",n);
    }
  
    return 0;
    
}

2.计算球的体积(3min)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2002

Problem Description
根据输入的半径值,计算球的体积。
Input
输入数据有多组,每组占一行,每行包括一个实数,表示球的半径。
Output
输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数。
Sample Input
1
1.5
Sample Output
4.189
14.137
题解:此题也异常简单,只要知道怎么计算球体积就可以 了,但是不要忘记三分之四的4要写成4.0.
#include<stdio.h>
#include<math.h>
#define PI 3.1415927
int main(void)
{
    double r;
    while(~scanf("%lf",&r))
    {
               double V=0;
               V=4.0/3*PI*r*r*r;
               printf("%0.3lf
",V);
    }
  
    return 0;
    
}

 3.成绩转换(4min)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2004

Problem Description
输入一个百分制的成绩t,将其转换成对应的等级,具体转换规则如下:
90~100为A;
80~89为B;
70~79为C;
60~69为D;
0~59为E;
Input
输入数据有多组,每组占一行,由一个整数组成。
Output
对于每组输入数据,输出一行。如果输入数据不在0~100范围内,请输出一行:“Score is error!”。
Sample Input
56
67
100
123
Sample Output
E
D
A
Score is error!
题解:此题也简单,五个循环就可以搞定。
#include<stdio.h>
#include<math.h>
int main(void)
{
    int  t;
    while(~scanf("%d",&t))
    {
        if(t>=90&&t<=100)
            printf("A
");
        else if(t>=80&&t<=89)
            printf("B
");
        else if(t>=70&&t<=79)
            printf("C
");
        else if(t>=60&&t<=69)
            printf("D
");
        else if(t>=0&&t<=59)
            printf("E
");
        else 
            printf("Score is error!
");
    }
  
    return 0;
    
}

 4.ASCII码排序(38min)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2000

Problem Description
输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。
Input
输入数据有多组,每组占一行,有三个字符组成,之间无空格。
Output
对于每组输入数据,输出一行,字符中间用一个空格分开。
Sample Input
qwe
 asd
zxc
Sample Output
e q w
a d s
c x z
题解:此题思路有三步:
                                   1.每次要先从控制台接收3个字符串,我是定义了一个字符数组接收三个字符串。
                                   2.把接收的三个字符串转成对应的ASCII按从小到大排序,我用的是选择排序。
                                   3.把转成ASCII的字符按从小到大顺序再转换成字符输出。
但是,但是,但是,你按照上面思路写的代码不一定对,因为scanf在接收字符时,换行也会被接收,所以在定义字符数组时,要多定义一个长度来存储换行,要不然,你总是得不到你想要的结果,问题就出在换行也时一个字符。
代码如下:
#include<stdio.h>
#include<math.h>
int main(void)
{
    char t[4];
    int  a[3];
    while(~scanf("%c%c%c%c",&t[0],&t[1],&t[2],&t[3]))
    {
        a[0]=t[0]-'0';
        a[1]=t[1]-'0';
        a[2]=t[2]-'0';
        
        int i;
        int temp;
        int j;
        int k;
        for(i=0;i<3;i++)
        {
            k=i;
            for(j=i;j<3;j++)
                 if(a[j]<a[k])
                      k=j;
                 temp=a[k];
                 a[k]=a[i];
                 a[i]=temp;
            
    }
    printf("%c %c %c
",a[0]+'0',a[1]+'0',a[2]+'0');
    
    }
  
    return 0;
    
}
 
原文地址:https://www.cnblogs.com/ping2yingshi/p/12241202.html