HDU100题简要题解(2020~2029)

HDU2020 绝对值排序

题目链接

Problem Description
输入n(n<=100)个整数,按照绝对值从大到小排序后输出。题目保证对于每一个测试实例,所有的数的绝对值都不相等。

Input
输入数据有多组,每组占一行,每行的第一个数字为n,接着是n个整数,n=0表示输入数据的结束,不做处理。

Output
对于每个测试实例,输出排序后的结果,两个数之间用一个空格隔开。每个测试实例占一行。

Sample Input
3 3 -4 2
4 0 1 2 -3
0

Sample Output
-4 3 2
-3 2 1 0

用一个结构体储存原本的值以及绝对值,然后以绝对值为标准进行排序,最后输出原值

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

int n;
struct node{
  int num;
  int absnum;
}a[101];

bool cmp(node x, node y) {
  return x.absnum > y.absnum;
}

int main() {
  while (scanf("%d", &n) != EOF) {
    if (n == 0) break;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i].num);
      a[i].absnum = abs(a[i].num);
    }
    sort(a + 1, a + 1 + n, cmp);
    for (int i = 1; i <= n; i++) 
      if (i != n) printf("%d ", a[i].num);
      else printf("%d\n", a[i].num);  
  }
  return 0;
}

HDU2021 发工资咯:)

题目链接

Problem Description
作为杭电的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的日子,养家糊口就靠它了,呵呵
但是对于学校财务处的工作人员来说,这一天则是很忙碌的一天,财务处的小胡老师最近就在考虑一个问题:如果每个老师的工资额都知道,最少需要准备多少张人民币,才能在给每位老师发工资的时候都不用老师找零呢?
这里假设老师的工资都是正整数,单位元,人民币一共有100元、50元、10元、5元、2元和1元六种。

Input
输入数据包含多个测试实例,每个测试实例的第一行是一个整数n(n<100),表示老师的人数,然后是n个老师的工资。
n=0表示输入的结束,不做处理。

Output
对于每个测试实例输出一个整数x,表示至少需要准备的人民币张数。每个输出占一行。

Sample Input
3
1 2 3
0

Sample Output
4

能拿最大的就要最大的,最大的钱给不开再给小的,就这样算就好了

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

int n, m, sum, ans;

int main() {
  while (scanf("%d", &n) != EOF) {
    if (n == 0) break;
    ans = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &m);
      sum = 0;
      sum += m / 100 + m % 100 / 50 + m % 100 % 50 / 10 + m % 100 % 50 % 10 / 5 + m % 100 % 50 % 10 % 5 / 2 + m % 100 % 50 % 10 % 5 % 2;
      ans += sum;
    }
    printf("%d\n", ans);
  }
  return 0;
}

HDU2022 海选女主角

题目链接

Problem Description
potato老师虽然很喜欢教书,但是迫于生活压力,不得不想办法在业余时间挣点外快以养家糊口。
“做什么比较挣钱呢?筛沙子没力气,看大门又不够帅...”potato老师很是无奈。
“张艺谋比你还难看,现在多有钱呀,听说还要导演奥运开幕式呢!你为什么不去娱乐圈发展呢?”lwg在一旁出主意。
嗯,也是,为了生存,就委屈点到娱乐圈混混吧,马上就拍一部激光电影《杭电记忆——回来我的爱》。
说干就干,马上海选女主角(和老谋子学的,此举可以吸引媒体的眼球,呵呵),并且特别规定,演员必须具有ac的基本功,否则直接out!
由于策划师风之鱼(大师级水王)宣传到位,来应聘的MM很多,当然包括nit的蛋糕妹妹等呼声很高的美女,就连zjut的jqw都男扮女装来应聘(还好被安全顾问hdu_Bin-Laden认出,给轰走了),看来娱乐圈比acm还吸引人哪...
面试那天,刚好来了mn个MM,站成一个mn的队列,副导演Fe(OH)2为每个MM打了分数,分数都是32位有符号整数。
一开始我很纳闷:分数怎么还有负的?Fe(OH)2解释说,根据选拔规则,头发染成黄色、化妆太浓、穿的太少等等都要扣分数的,扣的多了就可能是负分了,当然,如果发现话语中夹有日语,就直接给-2147483648分了。
分数送上来了,是我做决定的时候了,我的一个选拔原则是,要选一个面试分数绝对值(必须还是32位整数)最大的MM。
特别说明:如果不幸选中一个负分的MM,也没关系,因为我觉得,如果不能吸引你,那要想法恶心你。

Input
输入数据有多组,每组的第一行是两个整数m和n,表示应聘MM的总共的行列数,然后是m行整数,每行有n个,m和n的定义见题目的描述。

Output
对于每组输入数据,输出三个整数x,y和s,分别表示选中的MM的行号、列号和分数。
note:行号和列号从一开始,如果有多个MM的分数绝对值一样,那么输出排在最前面的一个(即行号最小的那个,如果行号相同则取列号最小的那个)。

Sample Input
2 3
1 4 -3
-7 3 0

Sample Output
2 1 -7

好长的题干......找一个绝对值最大的,不断更新最大值所在的行、列

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

int n, m, a[101][101], maxx, maxi, maxj;

int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
  maxx = 0;
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= m; j++) {
        scanf("%d", &a[i][j]);
        if (abs(a[i][j]) > maxx) {
          maxi = i;
          maxj = j;
          maxx = abs(a[i][j]);
        }
      }
    printf("%d %d ", maxi, maxj);
    printf("%d\n", a[maxi][maxj]);
  }
  return 0;
}

HDU2023 求平均成绩

题目链接

Problem Description
假设一个班有n(n<=50)个学生,每人考m(m<=5)门课,求每个学生的平均成绩和每门课的平均成绩,并输出各科成绩均大于等于平均成绩的学生数量。

Input
输入数据有多个测试实例,每个测试实例的第一行包括两个整数n和m,分别表示学生数和课程数。然后是n行数据,每行包括m个整数(即:考试分数)。

Output
对于每个测试实例,输出3行数据,第一行包含n个数据,表示n个学生的平均成绩,结果保留两位小数;第二行包含m个数据,表示m门课的平均成绩,结果保留两位小数;第三行是一个整数,表示该班级中各科成绩均大于等于平均成绩的学生数量。
每个测试实例后面跟一个空行。

Sample Input
2 2
5 10
10 20

Sample Output
7.50 15.00
7.50 15.00
1

先求出学生平均值、每门课的平均值,然后从头搜一遍求出人数

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

double n, m, singlesum[101], subjectsum[101], a[101][101];
int ans, cnt;

int main() {
  while (scanf("%lf%lf", &n, &m) != EOF) {
    memset(singlesum, 0, sizeof(singlesum));
    memset(subjectsum, 0, sizeof(subjectsum));
    for (int i = 1; i <= n; i++) 
      for (int j = 1; j <= m; j++) {
        scanf("%lf", &a[i][j]);
        singlesum[i] += a[i][j];
        subjectsum[j] += a[i][j];
      }
    for (int i = 1; i <= n; i++) 
      if (i != n) printf("%.2lf ", singlesum[i] / m);
      else printf("%.2lf\n", singlesum[i] / m);
    for (int i = 1; i <= m; i++)
      if (i != m) printf("%.2lf ", subjectsum[i] / n);
      else printf("%.2lf\n", subjectsum[i] / n);
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= m; j++) {
        if (a[i][j] >= subjectsum[j] / n) cnt++;
      }
      if (cnt == m) ans++;
      cnt = 0;
    }
    printf("%d\n\n", ans);
    ans = 0;
  }
  return 0;
}

HDU2024 C语言合法标识符

题目链接

Problem Description
输入一个字符串,判断其是否是C的合法标识符。

Input
输入数据包含多个测试实例,数据的第一行是一个整数n,表示测试实例的个数,然后是n行输入数据,每行是一个长度不超过50的字符串。

Output
对于每组输入数据,输出一行。如果输入数据是C的合法标识符,则输出"yes",否则,输出“no”。

Sample Input
3
12ajf
fi8x_a
ff ai_2

Sample Output
no
yes
no

判断一个制胡窜是不是合法的标识符,标识符合不合法应该在C语言课开始的几节课就讲过了吧

#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;

int n;

int main() {
  while (scanf("%d", &n) != EOF) {
    getchar();
    for (int i = 1; i <= n; i++) {
      char c[101];
      gets(c);
      int flag = 0;
      for (int j = 0; j < strlen(c); j++) {
        if(!((c[j] >= '0' && c[j] <= '9') || (c[j] >= 'A' && c[j] <= 'Z') || (c[j] >= 'a' && c[j] <= 'z') || (c[j] == '_'))) {
          flag = 1; 
          break;
        }
        if ((c[0] >= '0' && c[0] <= '9' && j == 0)) {
          flag = 1; 
          break;
        }
      }
      if (flag == 0) printf("yes\n");
      if (flag == 1) printf("no\n"); 
   }
  }
  return 0;
}

HDU2025 查找最大元素

题目链接

Problem Description
对于输入的每个字符串,查找其中的最大字母,在该字母后面插入字符串“(max)”。

Input
输入数据包括多个测试实例,每个实例由一行长度不超过100的字符串组成,字符串仅由大小写字母构成。

Output
对于每个测试实例输出一行字符串,输出的结果是插入字符串“(max)”后的结果,如果存在多个最大的字母,就在每一个最大字母后面都插入"(max)"。

Sample Input
abcdefgfedcba
xxxxx

Sample Output
abcdefg(max)fedcba
x(max)x(max)x(max)x(max)x(max)

记录下最大的字母,输出的时候在字母后面跟着输出(max)就好

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

char a[101], maxx;

int main() {
  while (gets(a)) {
    int len = strlen(a);
    maxx = a[0];
    for (int i = 0; i < len; i++) 
      if (maxx < a[i]) maxx = a[i];
      for (int i = 0; i < len; i++) {
  	cout << a[i];
  	if (a[i] == maxx) cout << "(max)";
      }
      cout << endl;
  }
  return 0;
}

HDU2026 首字母变大写

题目链接

Problem Description
输入一个英文句子,将每个单词的第一个字母改成大写字母。

Input
输入数据包含多个测试实例,每个测试实例是一个长度不超过100的英文句子,占一行。

Output
请输出按照要求改写后的英文句子。

Sample Input
i like acm
i want to get an accepted

Sample Output
I Like Acm
I Want To Get An Accepted

把第一个字母以及每个空格后的第一个字母变为大写即可

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

char a[101];

int main() {
  while (gets(a)) {
    int len = strlen(a);
    a[0] -= 32;
    for (int i = 0; i < len; i++) {
      if (a[i] == ' ') a[i + 1] -= 32;
      cout << a[i];
    }
    cout << endl;
  }
  return 0;
}

HDU2027 统计元音

题目链接

Problem Description
统计每个元音字母在字符串中出现的次数。

Input
输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串。

Output
对于每个测试实例输出5行,格式如下:
a:num1
e:num2
i:num3
o:num4
u:num5
多个测试实例之间由一个空行隔开。
请特别注意:最后一块输出后面没有空行:)

Sample Input
2
aeiou
my name is ignatius

Sample Output
a:1
e:1
i:1
o:1
u:1

a:2
e:1
i:3
o:0
u:1

统计每个元音出现的个数即可

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

int n, num1, num2, num3, num4, num5;
char a[101];

int main() {
  scanf("%d", &n);
  getchar();
  while (n--) {
    gets(a);
    num1 = num2 = num3 = num4 = num5 = 0;
    int len = strlen(a);
    for (int i = 0; i < len; i++) {
      if (a[i] == 'a') num1++;
      if (a[i] == 'e') num2++;
      if (a[i] == 'i') num3++;
      if (a[i] == 'o') num4++;
      if (a[i] == 'u') num5++;
    }
    printf("a:%d\ne:%d\ni:%d\no:%d\nu:%d\n", num1, num2, num3, num4, num5);
    if (n != 0) cout << endl;
  }
  return 0;
}

HDU2028 Lowest Common Multiple Plus

题目链接

Problem Description
求n个数的最小公倍数。

Input
输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数。

Output
为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行。你可以假设最后的输出是一个32位的整数。

Sample Input
2 4 6
3 2 5 7

Sample Output
12
70

求最小公倍数问题,关于最小公倍数,正好前不久刚在洛谷上做到一个题P1572 计算分数也是不错的

说实在的整理时看到自己的代码吃了一惊,不明白当时为什么要这么写......在原有代码上稍作改动,除去了一些冗杂的部分,提交后也是正确的

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

int n, x, y;

int gcd(int x, int y) {
  return y ? gcd(y, x % y) : x;
}

int main() {
  while (scanf("%d", &n) != EOF) {
    scanf("%d", &x);
    for (int i = 1; i < n; i++) {
      scanf("%d", &y);
      x = x / gcd(x, y) * y;
    }
    printf("%d\n", x);
  }
  return 0;
}

HDU2029 Palindromes _easy version

题目链接

Problem Description
“回文串”是一个正读和反读都一样的字符串,比如“level”或者“noon”等等就是回文串。请写一个程序判断读入的字符串是否是“回文”。

Input
输入包含多个测试实例,输入数据的第一行是一个正整数n,表示测试实例的个数,后面紧跟着是n个字符串。

Output
如果一个字符串是回文串,则输出"yes",否则输出"no".

Sample Input
4
level
abcde
noon
haha

Sample Output
yes
no
yes
no

回文串,很经典的东西

用另一个数组储存倒着的样子,然后逐位比较,最后一样的字母的和与制胡窜长度相同的话就是回文串了

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

int n;
char a[1001], b[1001];

int main() {
  while (scanf("%d", &n) != EOF) {
    getchar();
    while (n--) {
      int cnt = 0;
      gets(a);
      int len = strlen(a);
      for (int i = 0; i < len; i++)
  	b[i] = a[len - 1 - i];
      for (int i = 0; i < len; i++)
  	if (a[i] == b[i]) cnt++;
      if (cnt != len) cout << "no" << endl;
      else cout << "yes" << endl;
    }
  }
  return 0;
}
posted @ 2020-11-19 16:18  Moominn  阅读(636)  评论(0编辑  收藏  举报