hdu 2000-2010 ACM

2000:

输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。
 
Input
输入数据有多组,每组占一行,有三个字符组成,之间无空格。
 
Output
对于每组输入数据,输出一行,字符中间用一个空格分开。
 
Sample Input
qwe
asd
zxc
 
Sample Output
e q w
a d s
c x z
#include <stdio.h>
#include <string.h>
void exchange(char** a, char** b)
{
    char* c;
    c = *a;
    *a = *b;
    *b = c;
}
void SWAP(char** a, char** b, char** c)
{
    if (strcmp(*a, *b) > 0)
    {
        exchange(a, b);
    }
    if (strcmp(*a, *c) > 0)
    {
        exchange(a, c);
    }
    if (strcmp(*b, *c) > 0)
    {
        exchange(b, c);
    }


}
int main(int argc, char const* argv[])
{
    char* a, * b, * c;
    char j, k, l;
    while (scanf("%c%c%c", &j, &k, &l)!=EOF)
    {
        a = &j;
        b = &k;
        c = &l;
        SWAP(&a, &b, &c);
        printf("%c %c %c\n", *a, *b, *c);
        getchar();
    }
    printf("\n");
    return 0;
}
    
View Code

 

2001:

输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离。
 
Input
输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2,数据之间用空格隔开。
 
Output
对于每组输入数据,输出一行,结果保留两位小数。
 
Sample Input
0 0 0 1
0 1 1 0
 
Sample Output
1.00
1.41
 1 #include <stdio.h>  
 2 #include <math.h>  
 3   
 4 int main()  
 5 {  
 6 double x1,y1,x2,y2;  
 7 while(EOF != scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2))  
 8 {  
 9 printf("%.2f",sqrt(pow(x1-x2,2)+pow(y1-y2,2)));  
10 }  
11 }  
View Code

 2001心得:C语言中应该使用数学函数包中的pow(a,n)来表示a的n次方

 

2002:

Problem Description
根据输入的半径值,计算球的体积。
 
Input
输入数据有多组,每组占一行,每行包括一个实数,表示球的半径。
 
Output
输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数。
 
Sample Input
1
1.5
 
Sample Output
4.189
14.137
Hint
#define PI 3.1415927
#include <stdio.h>
#include <math.h>
#define PI 3.1415927
main()
{
    double r;
    double v;
    while (EOF != scanf("%lf", &r))
    {
        
        v = 4*PI*(pow(r,3)) / 3;
        
        printf("%.3f\n",v);
    }
    return 0;
}
View Code

2002心得:

使用宏定义:#define 宏名 宏体,这个替换的过程被称为“宏代换”或“宏展开”(macro expansion)。“宏代换”是由预处理程序自动完成的。宏定义和函数的最大差别就是:宏定义是原地展开,因此没有调用开销;而函数是跳转执行再返回,因此函数有比较大的调用开销。所以宏定义和函数相比,优势就是没有调用开销,没有传参开销,所以当函数体很短(尤其是只有一句话时)可以用宏定义来替代,这样效率高。

 

2003:

Problem Description
求实数的绝对值。
 
Input
输入数据有多组,每组占一行,每行包含一个实数。
 
Output
对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。
 
Sample Input
123
-234.00
 
Sample Output
123.00
234.00
#include <stdio.h>
#include <math.h>
main()
{
    double a;
    while (EOF != scanf("%lf", &a))
    {
        
        a = abs(a);
        
        printf("%.2f\n",a);
    }
    return 0;
}
View Code

 

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>


main()
{
    int a;
    while (EOF != scanf("%d", &a))
    {
        if (a>100)
            printf("Score is error\n");
        else if(a >= 90)
            printf("A\n");
        else if(a>=80)
            printf("B\n");
        else if (a >= 70)
            printf("C\n");
        else if (a >= 60)
            printf("D\n");
        else
            printf("E\n");


       
    }
    return 0;
}
View Code

 

2005:

Problem Description
给定一个日期,输出这个日期是该年的第几天。
 
Input
输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。
 
Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。 
 
Sample Input
1985/1/20
2006/3/12
 
Sample Output
20
71

使用switch语句的程序(看起来复杂):

#include <stdio.h>
#include <math.h>


main()
{
   
    int sum, year, month, day;
    while (EOF != scanf("%d/%d/%d", &year,&month,&day))
    
    {
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
            sum = 1;
        else 
            sum = 0;
        switch (month)
        {
        case 1:sum =0;
            break;
        case 2:sum = sum + 31;
            break;
        case 3:sum = sum +31+ 28;
            break;
        case 4:sum = sum + 28+31*2;
            break;
        case 5:sum = sum + 28 + 31*2+30;
            break;
        case 6:sum = sum + 28 + 31*3+30;
            break;
        case 7:sum = sum + 28 + 31*3+30*2;
            break;
        case 8:sum = sum + 28 + 31*4+30*2;
            break;
        case 9:sum = sum + 28 + 31 * 5 + 30 * 2;
            break;
        case 10:sum = sum + 28 + 31 * 5 + 30 * 3;
            break;
        case 11:sum = sum + 28 + 31 * 6 + 30 * 3;
            break;
        case 12:sum = sum + 28 + 31 * 6 + 30 * 4;
            break;

        }
        sum = sum + day;
        printf("%d\n", sum);

       
    }
    return 0;
}
View Code

使用数组(更推荐):

#include <stdio.h>
#include <math.h>


main()
{
    int a[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };   
    int sum, year, month, day;
    while (EOF != scanf("%d/%d/%d", &year,&month,&day))
    {
        sum = 0;
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
            a[2] = 29;
       
        for (int i = 1; i < month; i++)
        {
            sum = sum + a[i];
        }
        sum = sum + day;
        printf("%d\n", sum);
    }
}
View Code

 

2006:

Problem Description
给你n个整数,求他们中所有奇数的乘积。
 
Input
输入数据包含多个测试实例,每个测试实例占一行,每行的第一个数为n,表示本组数据一共有n个,接着是n个整数,你可以假设每组数据必定至少存在一个奇数。
 
Output
输出每组数中的所有奇数的乘积,对于测试实例,输出一行。
 
Sample Input
3 1 2 3
4 2 3 4 5
 
Sample Output
3
15
#include <stdio.h>
#include <math.h>


main()
{
    
    int  n,a;
    while (EOF != scanf("%d", &n))
    {
        int sum = 1;
       
        while (n--)
        {
            scanf("%d", &a);
            if (a % 2 == 0)
                sum = sum;
            else
                sum = sum * a;
            getchar();


        }
        printf("%d\n", sum);
    }
 }
View Code

心得:函数的镶嵌很是重要,一不留神就会出错,sum放在while里面外面意义不同

 

2007:

Problem Description
给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和。
 
Input
输入数据包含多组测试实例,每组测试实例包含一行,由两个整数m和n组成。
 
Output
对于每组输入数据,输出一行,应包括两个整数x和y,分别表示该段连续的整数中所有偶数的平方和以及所有奇数的立方和。
你可以认为32位整数足以保存结果。
 
Sample Input
1 3 2 5
 
Sample Output
4 28
20 152

调用函数:

#include <stdio.h>


main()
{
    int max(int x, int y);
    int  m,n,a;
    while (EOF != scanf("%d %d", &m,&n))
    {
        a = max(m, n);
        //printf("%d", a);
        int sum=0,sum2 = 0;
      
        for (int i = 1; i <= a; i++)
        {
            
            if (i % 2 == 0)

                sum = sum + i * i;
            
            else
                sum2 = sum2 + i * i * i;
            
        }
        getchar();
        printf("%d %d\n", sum,sum2);
    }
 }

int max(int x, int y)
{
    int z;
    if (x > y) z = x;
    else z = y;
    return z;
}
View Code

不调用函数直接写main:

#include <stdio.h>


main()
{
  
    int  t,m,n,a;
    while (EOF != scanf("%d %d", &m,&n))
    {
        int sum = 0, sum2 = 0;
        if (m > n)
        {
            t = m;
            m = n;
            n = t;
        }
        
      
        for (int i = m; i <= n; i++)
        {
            
            if (i % 2==1)
                sum2 += i * i * i;
            else
                sum += i * i;
            
        }
        getchar();
        printf("%d %d\n", sum,sum2);
    }
 }
View Code

 

心得:题干说的是一段整数m,n,意思是从m到n的整数区间内选择奇数偶数作业

 

2008:

Problem Description
统计给定的n个数中,负数、零和正数的个数。
 
Input
输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的个数,然后是n个实数;如果n=0,则表示输入结束,该行不做处理。
 
Output
对于每组输入数据,输出一行a,b和c,分别表示给定的数据中负数、零和正数的个数。
 
Sample Input
6 0 1 2 3 -1 0 5 1 2 3 4 0.5 0
 
Sample Output
1 2 3
0 0 5

路过的大神帮我看看这段代码为啥遇到n=0的情况不返回0值呢?好像卡住了一样?

#include <stdio.h>


main()
{

    float    m;
    int n,i, a, b, c;
    scanf("%d", &n);
    if (n == 0)
        return 0;
    while (n--)
    {
        a = b = c = 0;
        for (i = 0; i < n; i++)
        {
            scanf("%f",&m);
            if (m < 0)
                a++;
            else if (m > 0)
                c++;
            else
                b++;
        }
            printf("%d %d %d\n", a, b, c);

    }
     
    
}
View Code

可行代码:

# include<stdio.h>
int main()
{
    int a, b, d, flag1, flag2, flag3;
    double c;
    while (scanf("%d", &a))
    {
        if (a == 0)
        {
            break;
        }
        flag1 = 0; flag2 = 0; flag3 = 0;
        for (b = 0; b < a; b++)
        {
            scanf("%lf", &c);
            if (c > 0)
            {
                flag1++;
            }
            if (c == 0)
            {
                flag2++;
            }
            if (c < 0)
            {
                flag3++;
            }
        }
        printf("%d %d %d\n", flag3, flag2, flag1);
    }
    return 0;
}
View Code

 

2009:

Problem Description
数列的定义如下:
数列的第一项为n,以后各项为前一项的平方根,求数列的前m项的和。
 
Input
输入数据有多组,每组占一行,由两个整数n(n<10000)和m(m<1000)组成,n和m的含义如前所述。
 
Output
对于每组输入数据,输出该数列的和,每个测试实例占一行,要求精度保留2位小数。
 
Sample Input
81 4
2 2
 
Sample Output
94.73
3.41
# include<stdio.h>
# include<math.h>

int main()
{
    
    double a, b,flag;
    while (scanf("%lf%lf", &a, &b))
    {

        flag = 0;
        for (int i = 0; i <b; i++)
        {
            
            flag += a;
            a= sqrt(a);
        }
        printf("%.2lf\n", flag);

    }
}
View Code

 

 

2010:

Problem Description
春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:
“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=1^3+5^3+3^3。
现在要求输出所有在m和n范围内的水仙花数。
 
Input
输入数据有多组,每组占一行,包括两个整数m和n(100<=m<=n<=999)。
 
Output
对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须大于等于m,并且小于等于n,如果有多个,则要求从小到大排列在一行内输出,之间用一个空格隔开;
如果给定的范围内不存在水仙花数,则输出no;
每个测试实例的输出占一行。
 
Sample Input
100 120
300 380
 
Sample Output
no
370 371
#include<stdio.h>
int main()
{
    int i, m, n;

    while (scanf("%d %d", &m, &n) != EOF)
    {
        int a = 0, b = 0, c = 0, s = 0;
        int flag = 0;

        for (i = m; i <= n; i++)
        {
            a = i % 10;
            b = i / 10 % 10;
            c = i / 100 % 10;
            if (i == a * a * a + b * b * b + c * c * c)
            {
                s += 1;
                if (flag)
                    printf(" %d", i);
                else
                {
                    printf("%d", i);
                    flag = 1;
                }

            }

        }
        if (s == 0)printf("no\n");
        else printf("\n");
    }
}
View Code

 

知识又增加了一点!gaoxing!

posted @ 2020-06-05 01:40  热心市民陆女士  阅读(251)  评论(0编辑  收藏  举报