【算法练习】--- HDU-2000---2009

2000-ASCII码排序

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 188902    Accepted Submission(s): 75552

Problem Description

输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。

Input

输入数据有多组,每组占一行,有三个字符组成,之间无空格。

Output

对于每组输入数据,输出一行,字符中间用一个空格分开。

Sample Input

qwe
asd
zxc

Sample Output

e q w
a d s
c x z

解题思路

一道很简单的字符比较题,为了写的规范一点写了一个函数来比较,比较的是字符的ASCII码值。

AC代码

#include <stdio.h>
void fun(char *x, char *y)
{
if (*x > *y)
    {
        char tmp = *x;
        *x = *y;
        *y = tmp;
    }
}
int main()
{
    char a, b, c;
    while (scanf("%c%c%c", &a, &b, &c) != EOF)
    {
        fun(&a, &b);
        fun(&a, &c);
        fun(&b, &c);
        printf("%c %c %c\n", a, b, c);
    }
    return 0;
}

2001-计算两点间的距离

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 235306    Accepted Submission(s): 81655

Problem Description

输入两点坐标(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

解题思路

两点间距离公式

AC代码

#include<stdio.h>
#include<math.h>
void distance(float x1, float y1, float x2, float y2)
{
    float xx = (x1 - x2) * (x1 - x2);
    float yy = (y1 - y2) * (y1 - y2);
    printf("%.2f\n", sqrt(xx + yy));
}
int main()
{
    float x1 = 0.0f;
    float y1 = 0.0f;
    float x2 = 0.0f;
    float y2 = 0.0f;
    while (scanf("%f%f%f%f", &x1, &y1, &x2, &y2) != EOF)
    {
        distance(x1, y1, x2, y2);
    }
    return 0;
}

2002-计算球体积

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 178728    Accepted Submission(s): 70612

Problem Description

根据输入的半径值,计算球的体积。

Input

输入数据有多组,每组占一行,每行包括一个实数,表示球的半径。

Output

输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数。

Sample Input

1
1.5

Sample Output

4.189
14.137

Hint
#define PI 3.1415927

解题思路

球体体积公式

AC代码

#include<stdio.h>
#include<math.h>
#define PI 3.1415927
int main()
{
    double r = 0.0;
    while (scanf("%lf", &r) != EOF)
    {
        printf("%.3lf\n", 4.0 * PI * pow(r, 3) / 3);
    }
    return 0;
}

2003-求绝对值

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 148241    Accepted Submission(s): 71900

Problem Description

求实数的绝对值。

Input

输入数据有多组,每组占一行,每行包含一个实数。

Output

对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。

Sample Input

123
-234.00

Sample Output

123.00
234.00

AC代码

#include<stdio.h>
#include<math.h>
int main()
{
    double num = 0.0;
    while (EOF != scanf("%lf", &num))
    {
        printf("%.2lf\n", fabs(num));
    }
    return 0;
}

2004-成绩转换

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 161214    Accepted Submission(s): 70221

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!

解题思路

很简单一道题,第一次提交没有考虑到负数(因为我确实想不到要怎么考到负分)的情况。if…else或者switch都可以写。

AC代码

#include<stdio.h>
int main()
{
    int input = 0;
    while (EOF != scanf("%d", &input))
    {
        if (input < 0)
        {
            printf("Score is error!\n");
        }
        else 
        {
            switch (input / 10)
            {
            case 0:
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                printf("E\n");
                break;
            case 6:
                printf("D\n");
                break;
            case 7:
                printf("C\n");
                break;
            case 8:
                printf("B\n");
                break;
            case 9:
            case 10:
                printf("A\n");
                break;
            default:
                printf("Score is error!\n");
                break;
            }
        }
    }
    return 0;
}

2005-第几天?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 167303    Accepted Submission(s): 59230

Problem Description

给定一个日期,输出这个日期是该年的第几天。

Input

输入数据有多组,每组占一行,数据格式为YYYY/MM/DD组成,具体参见sample input ,另外,可以向你确保所有的输入数据是合法的。

Output

对于每组输入数据,输出一行,表示该日期是该年的第几天。

Sample Input

1985/1/20
2006/3/12

Sample Output

20
71

解题思路

计算日期是当年的第几天,因为题目明确输入都合法了所以只用考虑闰年的问题了,写一个判断闰年的函数就OK。

AC代码

#include<stdio.h>
int IsLeapYear(int year)
{
    if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
int main()
{
    int year = 0;
    int month = 0;
    int day = 0;
    int a[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    while (EOF != scanf("%d/%d/%d", &year, &month, &day))
    {
        int count = 0;
        if (IsLeapYear(year))
        {
            if(month > 2)
            count = 1;
        }
        for (int i = 0; i < month - 1; i++)
        {
            count += a[i];
        }
        count += day;
        printf("%d\n", count);
    }
    return 0;
}

2006-求奇数的乘积

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 105249    Accepted Submission(s): 64167

Problem Description

给你n个整数,求他们中所有奇数的乘积。

Input

输入数据包含多个测试实例,每个测试实例占一行,每行的第一个数为n,表示本组数据一共有n个,接着是n个整数,你可以假设每组数据必定至少存在一个奇数。

Output

输出每组数中的所有奇数的乘积,对于测试实例,输出一行。

Sample Input

3 1 2 3
4 2 3 4 5

Sample Output

3
15

AC代码

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

2007-平方和与立方和

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 188937    Accepted Submission(s): 59688

Problem Description

给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和。

Input

输入数据包含多组测试实例,每组测试实例包含一行,由两个整数m和n组成。

Output

对于每组输入数据,输出一行,应包括两个整数x和y,分别表示该段连续的整数中所有偶数的平方和以及所有奇数的立方和。
你可以认为32位整数足以保存结果。

Sample Input

1 3
2 5

Sample Output

4 28
20 152

解题思路

32位整数足以保存结果,所以数据类型用int就足够。

AC代码

#include<stdio.h>
void swap(int *x, int *y)
{
    if (*x > *y)
    {
        int tmp = *x;
        *x = *y;
        *y = tmp;
    }
}
int main()
{
    int left = 0;
    int right = 0;
    while (EOF != scanf("%d%d", &left, &right))
    {
        swap(&left, &right);
        int s1 = 0;
        int s2 = 0;
        for (int i = left; i <= right; i++)
        {
            if (i % 2 == 0)
            {
                s1 += (i * i);
            }
            else
            {
                s2 += (i * i * i);
            }
        }
        printf("%d %d\n", s1, s2);
    }
    return 0;
}

2008-数值统计

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 118559    Accepted Submission(s): 57010

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

AC代码

#include<stdio.h>
int main()
{
    int n = 0;
    double input = 0.0;
    while (EOF != scanf("%d", &n))
    {
        if (n == 0)
        {
            break;
        }
        int pos_num = 0;
        int neg_num = 0;
        int zer_num = 0;
        for (int i = 0; i < n; i++)
        {
            scanf("%lf", &input);
            if (input > 0)
            {
                pos_num++;
            }
            else if (input == 0)
            {
                zer_num++;
            }
            else
            {
                neg_num++;
            }
        }
        printf("%d %d %d\n", neg_num, zer_num, pos_num);
    }
    return 0;
}

2009-求数列的和

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 92152    Accepted Submission(s): 55596

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

AC代码

#include<stdio.h>
#include<math.h>
int main()
{
    double n = 0.0;
    int m = 0;
    while (EOF != scanf("%lf%d", &n, &m))
    {
        double sum = 0.0;
        for (int i = 0; i < m; i++)
        {
            sum += n;
            n = sqrt(n);
        }
        printf("%.2lf\n", sum);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Mr_HHHHH/article/details/78682704