HDU100题简要题解(2000~2009)

前言(废话)

从11月6号到11月20号,断断续续做了有三个星期,总算整完了,于是就闲下来慢慢整理汇总到这里

中间部分用到数学知识的十几道题边学边做直接把我这个数学菜鸟做到怀疑人生

有的题会重要写一下思路,大部分题题解应该会比较简单不会赘述(有的跨时太久可能也忘了

如有不正确的地方还请各位不吝赐教,感谢

下面进入正题

HDU2000 ASCII码排序

题目链接

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

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

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

Sample Input
qwe
asd
zxc

Sample Output
e q w
a d s
c x z

平平无奇的比较大小问题,直接上代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;

char a, b, c;

int main() {
  while (scanf("%c%c%c", &a, &b, &c) != EOF) {
    getchar();
    if (a > b) {
      char d = a;
      a = b;
      b = d;
    }
    if (a > c) {
      char d = a;
      a = c;
      c = d;
    }
    if (b > c) {
      char d = b;
      b = c;
      c = d;
    }
    printf("%c %c %c\n", a, b, c);
  }
  return 0;
}

HDU2001 计算两点间的距离

题目链接

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 <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

double x1, x2, z1, z2;
double ans;

int main() {
  while (scanf("%lf%lf%lf%lf", &x1, &z1, &x2, &z2) != EOF) {
    ans = sqrt((x1 - x2) * (x1 - x2) + (z1 - z2) * (z1 - z2));
    printf("%.2lf\n", ans);
  }
  return 0;
}

HDU2002 计算球体积

题目链接

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

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

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

Sample Input
1
1.5

Sample Output
4.189
14.137
Hint
"#define PI 3.1415927"

平平无奇的算数题

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#define PI 3.1415927
using namespace std;

double r, ans = 1.0;

int main() {
  while (scanf("%lf", &r) != EOF) {
    ans = PI * r * r * r * (4.0 / 3);
    printf("%.3lf\n", ans);
  }
  return 0;
}

HDU2003 求绝对值

题目链接

Problem Description
求实数的绝对值。

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

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

Sample Input
123
-234.00

Sample Output
123.00
234.00

字面意思,算绝对值

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;

double n, m;

int main() {
  while (scanf("%lf", &n) != EOF) {
    m = abs(n);
    printf("%.2lf\n", m);
  }
  return 0;
}

HDU2004 成绩转换

题目链接

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 <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;

int n;

int main() {
  while (scanf("%d", &n) != EOF) {
    if (n >= 90 && n <= 100) cout << "A" << endl;
    else if (n >= 80 && n <= 89) cout << "B" << endl;
    else if (n >= 70 && n <= 79) cout << "C" <<endl;
    else if (n >= 60 && n <= 69) cout << "D" << endl;
    else if (n >= 0 && n <= 59) cout << "E" << endl;
    else cout << "Score is error!" << endl;
  }
  return 0;
}

HDU2005 第几天?

题目链接

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

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

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

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

Sample Output
20
71

要判断这一年是不是闰年,由于鄙人贫乏的生活常识,还百度了一下什么是闰年......
需要两个数组,一个储存闰年每月的天数,一个储存平年每月的天数

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;

int x, y, z;
int day1[13] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int day2[13] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

bool check(int year) {
  if ((year % 4 ==0 && year % 100 != 0) || year % 400 == 0) return true;
  return false;
}

int main() {
  while (scanf("%d/%d/%d", &x, &y, &z) != EOF) {
    int ans = 0;
    if (check(x)) {
      for (int i = 0; i < y - 1; i++) ans += day1[i];
      ans += z;
      printf("%d\n", ans);
    } else {
      for (int i = 0; i < y - 1; i++) ans += day2[i];
      ans += z;
      printf("%d\n", ans);
    }
  }
  return 0;
}

HDU2006 求奇数的乘积

题目链接

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

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

Sample Input
3 1 2 3
4 2 3 4 5

Sample Output
3
15

平平无奇的算术题,就是把是奇数的数都乘起来

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;

int n, m;

int main() {
  while (scanf("%d", &n) != EOF) {
    int ans = 1;
    while (n--) {
      scanf("%d", &m);
      if (m % 2 == 1) ans *= m;
    }
    printf("%d\n", ans);
  }
  return 0;
}

HDU2007 平方和与立方和

题目链接

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

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

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

Sample Input
1 3
2 5

Sample Output
4 28
20 152

把区间内的偶数平方,把奇数立方,分别加起来

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;

int n, m;
long long ans1, ans2;

int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    ans1 = 0;
    ans2 = 0;
    if (n > m) {
      int x = n;
      n = m;
      m = x;
    }
    for (int i = n; i <= m; i++) {
      if (i % 2 == 0) ans1 += i * i;
      else ans2 += i * i * i;
    }
    printf("%lld %lld\n", ans1, ans2);
  }
  return 0;
}

HDU2008 数值统计

题目链接

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>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;

int n, ans1, ans2, ans3;
double m;

int main() {
  while (scanf("%d", &n) != EOF) {
    if (n == 0) break;
    ans1 = ans2 = ans3 = 0;
    while (n--) {
      scanf("%lf", &m);
      if (m > 0.0) ans3++;
      else if (m < 0.0) ans1++;
      else ans2++;
    }
    printf("%d %d %d\n", ans1, ans2, ans3);
  }
  return 0;
}

HDU2009 求数列的和

题目链接

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 <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;

double n, m, ans;

int main() {
  while (scanf("%lf%lf", &n, &m) != EOF) {
    m--;
    ans = n;
    while (m--) {
      ans += sqrt(n);
      n = sqrt(n);
    }
    printf("%.2lf\n", ans);
  }
  return 0;
}
posted @ 2020-11-19 12:48  Moominn  阅读(383)  评论(0编辑  收藏  举报