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

HDU100題簡要題解(2010~2019)

Problem Description
春天是鮮花的季節,水仙花就是其中最迷人的代表,數學上有個水仙花數,他是這樣定義的:
“水仙花數”是指一個三位數,它的各位數字的立方和等於其本身,比如:153=1^3+5^3+3^3。
現在要求輸出所有在m和n範圍內的水仙花數。
Input
輸入資料有多組,每組佔一行,包括兩個整數m和n(100<=m<=n<=999)。
Output
對於每個測試例項,要求輸出所有在給定範圍內的水仙花數,就是說,輸出的水仙花數必須大於等於m,並且小於等於n,如果有多個,則要求從小到大排列在一行內輸出,之間用一個空格隔開;
如果給定的範圍內不存在水仙花數,則輸出no;
每個測試例項的輸出佔一行。
Sample Input
100 120
300 380
Sample Output
no
370 371

水仙花數!懷念啊...夢迴初升高暑假集訓
把區間內的每個數的每一位的立方相加,與其本身相等即為水仙花數

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

int n, m;

int main() {
  while (scanf("%d%d", &m, &n) != EOF) {
    int flag = 0;
    for (int i = m; i <= n; i++) {
      int a = i % 10;
      int b = (i / 10) % 10;
      int c = i / 100;
      if (pow(a, 3) + pow(b, 3) + pow(c, 3) == i) {
        if (flag == 0) {
  	  printf("%d", i);
  	  flag = 1;
  	}
  	else printf(" %d", i);
      }
    }
    if (flag == 0) cout << "no" << endl;
    else cout << endl;
  }
  return 0;
} 

Problem Description
多項式的描述如下:
1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + ...
現在請你求出該多項式的前n項的和。
Input
輸入資料由2行組成,首先是一個正整數m(m<100),表示測試例項的個數,第二行包含m個正整數,對於每一個整數(不妨設為n,n<1000),求該多項式的前n項的和。
Output
對於每個測試例項n,要求輸出多項式前n項的和。每個測試例項的輸出佔一行,結果保留2位小數。
Sample Input
2
1 2
Sample Output
1.00
0.50

又是多項式求和,奇數項就加,偶數項就減

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

int n, m;
double sum;

int main() {
  while (scanf("%d", &n) != EOF) {
    for (int i = 1; i <= n; i++) {
      scanf("%d", &m);
      sum = 0;
      for (int j = 1; j <= m; j++) {
  	 if (j % 2 == 1) sum += 1.0 / j;
  	 else sum -= 1.0 / j;
      }
      printf("%.2lf\n", sum);
    }
  }
  return 0;
}

Problem Description
對於表示式n^2+n+41,當n在(x,y)範圍內取整數值時(包括x,y)(-39<=x<y<=50),判定該表示式的值是否都為素數。
Input
輸入資料有多組,每組佔一行,由兩個整數x,y組成,當x=0,y=0時,表示輸入結束,該行不做處理。
Output
對於每個給定範圍內的取值,如果表示式的值都為素數,則輸出"OK",否則請輸出“Sorry”,每組輸出佔一行。
Sample Input
0 1
0 0
Sample Output
OK

就把區間內的數都帶進表示式,看看錶達式的值是否為素數,如果不是,就直接輸出“Sorry”就好了,後面的沒必要繼續搜

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

int x, y;

bool check(int n) {
  for (int i = 2; i * i <= n; i++) {
    if (n % i == 0) return false;
  }
  return true;
}

int main() {
  while (scanf("%d%d", &x, &y) != EOF) {
    int flag = 0;
    if (x == 0 && y == 0) break;
    for (int i = x; i <= y; i++) {
      int sum = pow(i, 2) + i + 41;
      if (!check(sum)) {
        cout << "Sorry" << endl;
  	 flag = 1;
  	 break;
      }
    }
    if (flag == 0) cout << "OK" << endl; 
  }
  return 0;
}

Problem Description
喜歡西遊記的同學肯定都知道悟空偷吃蟠桃的故事,你們一定都覺得這猴子太鬧騰了,其實你們是有所不知:悟空是在研究一個數學問題!
什麼問題?他研究的問題是蟠桃一共有多少個!
不過,到最後,他還是沒能解決這個難題,呵呵^-^
當時的情況是這樣的:
第一天悟空吃掉桃子總數一半多一個,第二天又將剩下的桃子吃掉一半多一個,以後每天吃掉前一天剩下的一半多一個,到第n天準備吃的時候只剩下一個桃子。聰明的你,請幫悟空算一下,他第一天開始吃的時候桃子一共有多少個呢?
Input
輸入資料有多組,每組佔一行,包含一個正整數n(1<n<30),表示只剩下一個桃子的時候是在第n天發生的。
Output
對於每組輸入資料,輸出第一天開始吃的時候桃子的總數,每個測試例項佔一行。
Sample Input
2
4
Sample Output
4
22

從後倒著往前算,因為每天吃掉前一天剩下的桃子數的一半多一個,所以可以得到:
最後一天1個桃子,倒數第二天3個桃子,倒數第三天8個桃子......倒數第n天的桃子數 = 2 * (倒數第n-1天的桃子數 + 1)
從而得到第一天的桃子數

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

int n, sum = 1;

int main() {
  while (scanf("%d", &n) != EOF) {
    n--;
    sum = 1;
    while (n--) sum = 2 * (sum + 1);
      printf("%d\n", sum);
  }
  return 0;
}
  • HDU2014 青年歌手大獎賽_評委會打分
    題目連結

Problem Description
青年歌手大獎賽中,評委會給參賽選手打分。選手得分規則為去掉一個最高分和一個最低分,然後計算平均得分,請程式設計輸出某選手的得分。
Input
輸入資料有多組,每組佔一行,每行的第一個數是n(2<n<=100),表示評委的人數,然後是n個評委的打分。
Output
對於每組輸入資料,輸出選手的得分,結果保留2位小數,每組輸出佔一行。
Sample Input
3 99 98 97
4 100 99 98 97
Sample Output
98.00
98.50

按照題目要求做就可以

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

int n;
double a[101], sum, maxx, minn;

int main() {
  while (scanf("%d", &n) != EOF) {
    sum = 0;
    maxx = 0;
    minn = 1e7 + 1;
    for (int i = 1; i <= n; i++) {
      scanf("%lf", &a[i]);
      maxx = max(maxx, a[i]);
      minn = min(minn, a[i]);
      sum += a[i];
    }
    double ans = (sum - maxx - minn) / (n - 2);
    printf("%.2lf\n", ans);
  }
  return 0;
}

Problem Description
有一個長度為n(n<=100)的數列,該數列定義為從2開始的遞增有序偶數,現在要求你按照順序每m個數求出一個平均值,如果最後不足m個,則以實際數量求平均值。程式設計輸出該平均值序列。
Input
輸入資料有多組,每組佔一行,包含兩個正整數n和m,n和m的含義如上所述。
Output
對於每組輸入資料,輸出一個平均值序列,每組輸出佔一行。
Sample Input
3 2
4 2
Sample Output
3 6
3 7

就是有一個長度為n的每一項都為偶數的數列,然後讓你每m個數求一次平均值(不足m個時就以其實際數量求),最後輸出平均值的序列
用一個cnt記錄一下區間內數的個數,用sum記錄區間內的和,每到m就輸出一次平均值,然後兩者再從0開始記錄

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

int n, m;

int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    int sum = 0;
    int cnt = 0;
    for (int i = 2; i <= 2 * n; i += 2) {
      cnt++;
      sum += i;
      if (cnt == m && i != 2 * n) {
  	 cnt = 0;
  	 printf("%d ", sum / m);
  	 sum = 0;
      }
    }
    if (sum != 0) printf("%d\n", sum /cnt);
  }
  return 0;
}

Problem Description
輸入n(n<100)個數,找出其中最小的數,將它與最前面的數交換後輸出這些數。
Input
輸入資料有多組,每組佔一行,每行的開始是一個整數n,表示這個測試例項的數值的個數,跟著就是n個整數。n=0表示輸入的結束,不做處理。
Output
對於每組輸入資料,輸出交換後的數列,每組輸出佔一行。
Sample Input
4 2 1 3 4
5 5 4 3 2 1
0
Sample Output
1 2 3 4
1 4 3 2 5

沒什麼好說的......就是找到最小的和第一個交換一下位置然後輸出就好了

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

int n, a[101];

int main() {
  while (scanf("%d", &n) != EOF) {
    int cnt = 1;
    int minn = 1e7 + 1;
    for (int i = 1; i <= n; i++) {
      scanf("%d", &a[i]);
      if (a[i] < minn) {
  	 minn = a[i];
  	 cnt = i;
      }
    }
    int num = a[1];
    a[1] = a[cnt];
    a[cnt] = num;
    for (int i = 1; i <= n; i++) {
      if (i != n) printf("%d ", a[i]);
      else printf("%d\n", a[i]);
    }
  }
  return 0;
}

Problem Description
對於給定的一個字串,統計其中數字字元出現的次數。
Input
輸入資料有多行,第一行是一個整數n,表示測試例項的個數,後面跟著n行,每行包括一個由字母和數字組成的字串。
Output
對於每個測試例項,輸出該串中數值的個數,每個輸出佔一行。
Sample Input
2
asdfasdf123123asdfasdf
asdf111111111asdfasdfasdf
Sample Output
6
9

就是統計一個制胡竄裡有多少個數字唄,這裡用到了isdigit()去判斷數字

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

int n;
char c[10001];

int main() {
  while (scanf("%d", &n) != EOF) {
    while (n--) {
      int cnt = 0;
      scanf("%s", c);
      for (int i = 0; i < strlen(c); i++) 
  	 if (isdigit(c[i])) cnt++;
      printf("%d\n", cnt);
    }
  }
  return 0;
}

Problem Description
有一頭母牛,它每年年初生一頭小母牛。每頭小母牛從第四個年頭開始,每年年初也生一頭小母牛。請程式設計實現在第n年的時候,共有多少頭母牛?
Input
輸入資料由多個測試例項組成,每個測試例項佔一行,包括一個整數n(0<n<55),n的含義如題目中描述。
n=0表示輸入資料的結束,不做處理。
Output
對於每個測試例項,輸出在第n年的時候母牛的數量。
每個輸出佔一行。
Sample Input
2
4
5
0
Sample Output
2
4
6

小學奧數,當時就煩這類題目
這裡用了遞推,因為每隻小牛在第四個年頭也會開始生牛,所以第n年的奶牛數 = 第n-1年的奶牛數 + 第n-3年的奶牛數
即a[i] = a[i - 1] + a[i - 3];
注意要初始化a[1] = 1; a[2] = 2; a[3] = 3; a[4] = 4

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

int n, a[56];

int main() {
  while (scanf("%d", &n) != EOF) {
    a[1] = 1;
    a[2] = 2;
    a[3] = 3;
    a[4] = 4;
    if (n == 0) break;
    for (int i = 5; i <= n; i++) {
      a[i] = a[i - 1] + a[i - 3];
    }
    printf("%d\n", a[n]);
  }
  return 0;
}

Problem Description
有n(n<=100)個整數,已經按照從小到大順序排列好,現在另外給一個整數x,請將該數插入到序列中,並使新的序列仍然有序。
Input
輸入資料包含多個測試例項,每組資料由兩行組成,第一行是n和m,第二行是已經有序的n個數的數列。n和m同時為0標示輸入資料的結束,本行不做處理。
Output
對於每個測試例項,輸出插入新的元素後的數列。
Sample Input
3 3
1 2 4
0 0
Sample Output
1 2 3 4

先把要加的數放在最後,然後sort一下再輸出就好了

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

int n, x, a[101];

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