HDU2001-2010题解

本文详细解析了HDU在线评测平台的前10题,涵盖了ASCII码排序、计算两点间距离、球体积计算等基础算法题目,适合初学者入门实践。

HDU入门100题,可以用来熟悉OJ。


其中,HDU2000-2007来自C语言程序设计练习(一)。

2000 ASCII码排序

  • Problem Description
    输入三个字符后,按各字符的ASCII码从小到大的顺序输出这三个字符。
  • Input
    输入数据有多组,每组占一行,有三个字符组成,之间无空格。
  • Output
    对于每组输入数据,输出一行,字符中间用一个空格分开。
  • Sample Input
    qwe
    asd
    zxc
    
  • Sample Output
    e q w
    a d s
    c x z
    

scanf的写法可以换成cin >> a >> b >> c;这里是冒泡排序的思想。另外发现HDU无法使用万能头文件<bits/stdc++.h>。当然,也可以直接存入字符串,然后对字符串内的字符进行排序。

#include <cstdio>
#include <algorithm>
using namespace std;
int main() {
	char a, b, c;
	while (~scanf("%c %c %c", &a, &b, &c)) {
		getchar(); //吸收换行 
		if (a > b) swap(a, b);
		if (a > c) swap(a, c);
		if (b > c) swap(b, c);
		//a b c从小到大排列
		printf("%c %c %c\n", a, b, c); 
	}
	return 0;
}

2001 计算两点间的距离

  • 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
    
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
	double x1, x2, y1, y2;
	while (cin >> x1 >> y1 >> x2 >> y2) {
		double ans = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
		printf("%.2lf\n", ans);
	}
	return 0;
} 

2002 计算球体积

  • Problem Description
    根据输入的半径值,计算球的体积。
  • Input
    输入数据有多组,每组占一行,每行包括一个实数,表示球的半径。
  • Output
    输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数。
  • Sample Input
    1
    1.5
    
  • Sample Output
    4.189
    14.137
    
  • Hint
    #define PI 3.1415927

知道球的体积为V=(4/3)πr3

#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
#define PI 3.1415927
int main() {
	double r;
	while (cin >> r) {
		double ans = 4.0 / 3.0 * PI * r * r * r;
		printf("%.3lf\n", ans);
	}
	return 0;
} 

2003 求绝对值

  • Problem Description
    求实数的绝对值。
  • Input
    输入数据有多组,每组占一行,每行包含一个实数。
  • Output
    对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。
  • Sample Input
    123
    -234.00
    
  • Sample Output
    123.00
    234.00
    
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
	double r;
	while (cin >> r) {
		printf("%.2lf\n", r >= 0.0 ? r : -r);
	}
	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!”。
  • Sample Input
    56
    67
    100
    123
    
  • Sample Output
    E
    D
    A
    Score is error!
    
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;
#define PI 3.1415926535
int main() {
	int g;
	while (cin >> g) {
		if (g > 100 || g < 0) printf("Score is error!\n");
		else if (g >= 90) printf("A\n");
		else if (g >= 80) printf("B\n");
		else if (g >= 70) printf("C\n");
		else if (g >= 60) printf("D\n");
		else if (g >= 0) printf("E\n");
	}
	return 0;
} 

2005 第几天

这些题不放题目了,太简单了。

第一种方法,累积天数。

#include <cstdio> 
bool isLeap(int y) {
	return (y % 400 == 0) || (y % 4 == 0 && y % 100 != 0);
}
int days[2][13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
				   0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //1为闰年 
int main() {
	int yy, mm, dd;
	while (~scanf("%d/%d/%d", &yy, &mm, &dd)) {
		int ans = 1, y = yy, m = 1, d = 1;
		while (m < mm || d < dd) {
			d++; ans++;
			if (d > days[isLeap(yy)][m]) { //到了当前月的最大天数 
				d = 1; m++;
			}  
		}
		printf("%d\n", ans);
	} 
	return 0;
}

第二种方法,累积月份到该月份,再加上题目中该月给出的天数。

#include <cstdio> 
bool isLeap(int y) {
	return (y % 400 == 0) || (y % 4 == 0 && y % 100 != 0);
}
int days[2][13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,
				   0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //1为闰年 
int main() {
	int yy, mm, dd;
	while (~scanf("%d/%d/%d", &yy, &mm, &dd)) {
		int ans = 0, m = 1;
		while (m < mm) {
			ans += days[isLeap(yy)][m];
			m++;
		} 
  		ans += dd;
		printf("%d\n", ans);
	} 
	return 0;
}

2006 求奇数的乘积

用位运算判断奇数,会快一些;另外这道题用int可以过,不会溢出。

#include <cstdio>
int main() {
	int n;
	while (~scanf("%d", &n)) {
		int ans = 1, t;
		for (int i = 0; i < n; i++) {
			scanf("%d", &t);
			if (t & 1) ans *= t;
		}
		printf("%d\n", ans);
	}
	return 0;
}

2007 平方和与立方和

这种坑题做了很多遍了,要注意给出的两个数不一定按大小顺序。

#include <cstdio>
void swap(int &a, int &b) {
	int t = a; a = b; b = t;
}
int main() {
	int m, n;
	while (~scanf("%d%d", &m, &n)) {
		if (m > n) swap(m, n);
		int o = 0, e = 0;
		for (int i = m; i <= n; i++) {
			if (i & 1) o += (i * i * i);
			else e += (i * i);
		}
		printf("%d %d\n", e, o);
	}
	return 0;
}

2008 数值统计

实数的比较有时候是件很烦人的事情,但这道题只是简单的比较。

#include <cstdio>
int main() {
	int N;
	while (~scanf("%d", &N) && N != 0) {
		int n, z, p; n = z = p = 0;
		double t;
		for (int i = 0; i < N; i++) {
			scanf("%lf", &t);
			if (t < 0.0) n++; 
			else if (t > 0.0) p++;
			else z++;
		} 
		printf("%d %d %d\n", n, z, p);
	}
	return 0;
}

2009 求数列的和

#include <cstdio>
#include <cmath>
int main() {
	double n;
	int m;
	while (~scanf("%lf%d", &n, &m)) {
		double ans = 0.0;
		while (m--) {
			ans += n;
			n = sqrt(n);
		}
		printf("%.2lf\n", ans);
	}
	return 0;
}

2010 水仙花数

第一个想到的就是枚举每一位,三重循环。当然,也可以从m到n(m<=n)枚举每个数…似乎更加简洁一点…

#include <cstdio>
#include <cmath>
int main() {
	int m, n;
	while (~scanf("%d%d", &m, &n)) {
		int flag = false;
		for (int i = 1; i <= 9; i++) {
			for (int j = 0; j <= 9; j++) {
				for (int k = 0; k <= 9; k++) {
					int num = i * 100 + j * 10 + k;
					if (num >= m && num <= n && num == pow(i, 3) + pow(j, 3) + pow(k, 3)) {
					   if (!flag) printf("%d", num);
					   else printf(" %d", num);
					   flag = true;
  			        }
				}
			}
		}
		if (flag) printf("\n");
		if (!flag) printf("no\n");
	}
	return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

memcpy0

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值