杭电oj2001~2010


2001-计算两点间的距离

Problem Description
输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离。

Input
输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2,数据之间用空格隔开。

Output
对于每组输入数据,输出一行,结果保留两位小数。

Example
Input
0 0 0 1
0 1 1 0

Output
1.00
1.41

思路要点
两点距离公式

AC题解

#include <stdio.h>
#include <math.h>
int main()
{
    double x1, x2, y1, y2, l;
    while(~scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2))
    {
        l = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));//两点距离公式
        printf("%.2lf\n", l);
    }
    return 0;
}

2002-计算球体积

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

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

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

Example
Input
1
1.5

Output
4.189
14.137

Note
#define PI 3.1415927

思路要点
球的体积公式

AC题解

#include <stdio.h>
#include <math.h>
#define pi 3.1415927
int main()
{
    double r, v;
    while(~scanf("%lf", &r))
    {
        v = (4 * pi * r * r * r)/3;
        printf("%.3lf\n", v);
    }
    return 0;
}

2003-求绝对值

Problem Description
求实数的绝对值。

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

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

Example
Input
123
-234.00

Output
123.00
234.00

思路要点
fabs函数

AC题解

#include <stdio.h>
#include <math.h>
int main()
{
    double n;
    while(~scanf("%lf", &n))
    {
        printf("%.2lf\n", 1.0 * fabs(n));
    }
    return 0;
}

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!”。

Example
Input
56
67
100
123

Output
E
D
A
Score is error!

思路要点
switch函数

AC题解

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

2005-第几天?

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

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

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

Example
Input
1985/1/20
2006/3/12

Output
20
71

思路要点
判断是否为闰年

AC题解

#include <stdio.h>
int mm[50] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30 ,31, 30, 31};
int main()
{
    int y, m, d, ds;
    while(~scanf("%d/%d/%d", &y, &m, &d))
    {
        ds = 0;
        if(m == 1) printf("%d\n", d);
        else
        {
            for (int i = 1; i < m; i ++)
                ds += mm[i];
            if((y%400 == 0 || y %4 == 0 && y%100 != 0) && m != 2) ds ++;
            ds += d;
            printf("%d\n", ds);
        }
    }
    return 0;
}

2006-求奇数的乘积

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

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

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

Example
Input
3 1 2 3
4 2 3 4 5

Output
3
15

思路要点
判断奇偶数的几种方法
①求余数,若 x%2 == 0 则x为偶数,若 x%2 != 0 则x为奇数
②位运算,若 x&1 为真则x为奇数,为假则x为偶数

AC题解

#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	while(~scanf("%d", &n)){
		int m, s = 1;
		for(int i = 0; i < n; i ++){
			scanf("%d", &m);
			if(m%2 == 1) s *= m;
		}
		printf("%d\n", s);
	}
	return 0;
}

2007-平方和与立方和

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

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

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

Example
Input
1 3
2 5

Output
4 28
20 152

思路要点
for & if

AC题解

#include <stdio.h>
int main()
{
    long long a, b, s1, s2, t;
    while(~scanf("%lld%lld", &a, &b))
    {
        if(b < a)
        {
            t = a;
            a = b;
            b = t;
        }
        s1 = s2 = 0;
        for(int i = a; i <= b; i ++)
        {
            if(i%2 == 0) s1 += i * i;
            else s2 += i * i * i;
        }
        printf("%lld %lld\n", s1, s2);
    } 
}

2008-数值统计

Problem Description
统计给定的n个数中,负数、零和正数的个数。

Input
输入数据有多组,每组占一行,每行的第一个数是整数n(n<100),表示需要统计的数值的个数,然后是n个实数;如果n=0,则表示输入结束,该行不做处理。

Output
对于每组输入数据,输出一行a,b和c,分别表示给定的数据中负数、零和正数的个数。

Example
Input
6 0 1 2 3 -1 0
5 1 2 3 4 0.5
0

Output
1 2 3
0 0 5

思路要点
for & if

AC题解

#include <stdio.h>
int main()
{
    int n1, n2, n3, n;
    double t;
    while(~scanf("%d", &n))
    {
        if (n == 0) break;
        n1 = n2 = n3 = 0;
        for(int i = 1; i <= n; i ++)
        {
            scanf("%lf", &t);
            if (t < 0) n1 ++;
            if (t == 0) n2 ++;
            if (t > 0) n3 ++;
        }
        printf("%d %d %d\n", n1, n2, n3);
    }
    return 0;
}

2009-求数列的和

Problem Description
数列的定义如下:
数列的第一项为n,以后各项为前一项的平方根,求数列的前m项的和。

Input
输入数据有多组,每组占一行,由两个整数n(n<10000)和m(m<1000)组成,n和m的含义如前所述。

Output
对于每组输入数据,输出该数列的和,每个测试实例占一行,要求精度保留2位小数。

Example
Input
81 4
2 2

Output
94.73
3.41

思路要点
for & sqrt函数

AC题解

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

2010-水仙花数

Problem Description
春天是鲜花的季节,水仙花就是其中最迷人的代表,数学上有个水仙花数,他是这样定义的:
“水仙花数”是指一个三位数,它的各位数字的立方和等于其本身,比如:153=13+53+3^3。
现在要求输出所有在m和n范围内的水仙花数。

Input
输入数据有多组,每组占一行,包括两个整数m和n(100<=m<=n<=999)。

Output
对于每个测试实例,要求输出所有在给定范围内的水仙花数,就是说,输出的水仙花数必须大于等于m,并且小于等于n,如果有多个,则要求从小到大排列在一行内输出,之间用一个空格隔开;
如果给定的范围内不存在水仙花数,则输出no;
每个测试实例的输出占一行。

Example
Input
100 120
300 380

Output
no
370 371

思路要点
将三位数分开
注:此题对输出格式要求严格,最后一个输出后面不能带空格

AC题解

#include <stdio.h>
int main()
{
	int n, m, a, b, c, flag;
	while (~scanf("%d%d", &n, &m))
	{
		flag = 1;
		for (int i = n; i <= m; i ++)
		{
			a = i/100;
			b = i%100/10;
			c = i%10;
			if (i == a * a * a + b * b * b + c * c * c)
			{
				if (flag) printf("%d", i);
				else printf (" %d", i);
				flag = 0;
			}
		}
		if(flag) printf ("no");
		printf("\n");
	}
	return 0;
}

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值