本文记录了刷杭电 OJ2000-2009 的过程和一些想法,代码仅供参考!
Problem Description
输入三个字符后,按各字符的 ASCII 码从小到大的顺序输出这三个字符。
Input
输入数据有多组,每组占一行,有三个字符组成,之间无空格。
Output
对于每组输入数据,输出一行,字符中间用一个空格分开。
Sample Input
qwe asd zxc
Sample Output
e q w a d s c x z
解题思路
简单题,利用冒泡排序思想,直接比较交换
参考源码
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
char a, b, c;
while (cin >> a >> b >> c) {
if (a > b) swap(a, b); //逆序则交换
if (b > c) swap(b, c);
if (a > b) swap(a, b);
cout << a << " " << b << " " << c << endl;
}
return 0;
}
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 <iostream>
using namespace std;
int main() {
double x1, y1, x2, y2, dis;
while (cin >> x1 >> y1 >> x2 >> y2) {
dis = sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
printf("%.2f\n", dis);
}
return 0;
}
Problem Description
根据输入的半径值,计算球的体积。
Input
输入数据有多组,每组占一行,每行包括一个实数,表示球的半径。
Output
输出对应的球的体积,对于每组输入数据,输出一行,计算结果保留三位小数。
Sample Input
1 1.5
Sample Output
4.189 14.137
解题思路
简单题,按照球的体积公式直接计算
参考源码
#include <iostream>
#define PI 3.1415927
using namespace std;
int main() {
double r;
while (cin >> r) {
printf("%.3f\n", 1.0 * 4 / 3 * PI * r * r * r);
}
return 0;
}
Problem Description
求实数的绝对值。
Input
输入数据有多组,每组占一行,每行包含一个实数。
Output
对于每组输入数据,输出它的绝对值,要求每组数据输出一行,结果保留两位小数。
Sample Input
123 -234.00
Sample Output
123.00 234.00
解题思路
简单题,fabs()直接来就行
参考源码
#include <cmath>
#include <iostream>
using namespace std;
int main() {
double n;
while (cin >> n) {
printf("%.2f\n", fabs(n));
}
return 0;
}
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 使用
参考源码
#include<iostream>
using namespace std;
int main(){
int a;
while(cin >> a){
if(a > 100 || a < 0){
cout << "Score is error!" << endl;
}
else if(a >= 90 && a <= 100){
cout << "A" <<endl;
}
else if(a >= 80 && a <= 89){
cout << "B" <<endl;
}
else if(a >= 70 && a <= 79){
cout << "C" <<endl;
}
else if(a >= 60 && a <= 69){
cout << "D" <<endl;
}
else{
cout << "E" <<endl;
}
}
return 0;
}
Problem Description
给定一个日期,输出这个日期是该年的第几天。
Input
输入数据有多组,每组占一行,数据格式为 YYYY/MM/DD 组成,具体参见 sample input , 另外,可以向你确保所有的输入数据是合法的。
Output
对于每组输入数据,输出一行,表示该日期是该年的第几天。
Sample Input
1985/1/20 2006/3/12
Sample Output
20 71
解题思路
根据输入月份 m 计算前 m-1 个月的总天数,再加上天数,2 月份则判断是否为闰年 普通闰年:公历年份是 4 的倍数,且不是 100 的倍数的 世纪闰年:公历年份是整百数的,必须是 400 的倍数才是闰年
参考源码
#include <iostream>
using namespace std;
int main() {
int y, m, d;
int ans, days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char ch; //读取 /
while (cin >> y >> ch >> m >> ch >> d) {
ans = 0;
for (int i = 0; i < m - 1; i++) {
ans += days[i]; //计算 m-1 个月的总天数
}
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
if (m > 2) ans++; //闰年加一天
cout << ans + d << endl; //加上天数
}
return 0;
}
Problem Description
给你 n 个整数,求他们中所有奇数的乘积。
Input
输入数据包含多个测试实例,每个测试实例占一行,每行的第一个数为 n,表示本组数据一共有 n 个,接着是 n 个整数,你可以假设每组数据必定至少存在一个奇数。
Output
输出每组数中的所有奇数的乘积,对于测试实例,输出一行。
Sample Input
3 1 2 3 4 2 3 4 5
Sample Output
3 15
解题思路
简单题,判断输入是否为奇数
参考源码
#include <iostream>
using namespace std;
int main() {
int n, num, ans;
while (cin >> n) {
ans = 1;
while (n--) {
cin >> num;
if (num % 2 != 0) ans *= num;
}
cout << ans << endl;
}
return 0;
}
Problem Description
给定一段连续的整数,求出他们中所有偶数的平方和以及所有奇数的立方和。
Input
输入数据包含多组测试实例,每组测试实例包含一行,由两个整数 m 和 n 组成。
Output
对于每组输入数据,输出一行,应包括两个整数 x 和 y,分别表示该段连续的整数中所有偶数的平方和以及所有奇数的立方和。 你可以认为 32 位整数足以保存结果。
Sample Input
1 3 2 5
Sample Output
4 28 20 152
解题思路
简单题,判断奇数和偶数,分别求立方和和平方和
参考源码
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
int m, n, ans1, ans2;
while (cin >> m >> n) {
ans1 = ans2 = 0;
if (m > n) swap(m, n); //注意 m,n大小
for (int i = m; i <= n; i++) {
if (i % 2 == 0)
ans1 += i * i; //平方和
else
ans2 += i * i * i; //立方和
}
cout << ans1 << " " << ans2 << endl;
}
return 0;
}
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
解题思路
简单题,判断负数、零和正数并计数
参考源码
#include <iostream>
using namespace std;
int main() {
int n, c1, c2, c3;
double num;
while (cin >> n && n) {
c1 = c2 = c3 = 0;
while (n--) {
cin >> num;
if (num < 0)
c1++;
else if (num == 0)
c2++;
else
c3++;
}
cout << c1 << " " << c2 << " " << c3 << endl;
}
return 0;
}
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
解题思路
简单题,遍历 m 次循环求和
参考源码
#include <cmath>
#include <iostream>
using namespace std;
int main() {
double n, m, ans;
while (cin >> n >> m) {
ans = 0;
for (int i = 0; i < m; i++) { //前 m 项
ans += n;
n = sqrt(n); //更新
}
printf("%.2f\n", ans);
}
return 0;
}