1. 程式人生 > 實用技巧 >HDU100題簡要題解(2020~2029)

HDU100題簡要題解(2020~2029)

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;
}

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;
}

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;
}

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;
}

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;
}

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;
}

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
(這裡有個空行,為了排版我加了這行字qwq)
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;
}

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;
}

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;
}