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

HDU100題簡要題解(2000~2009)

前言(廢話)

從11月6號到11月20號,斷斷續續做了有三個星期,總算整完了,之後會慢慢整理彙總到這裡
中間部分用到數學知識的十幾道題邊學邊做直接把我這個數學菜鳥做到懷疑人生
11.6~11.10又恰逢暨南大學華為杯網路安全挑戰賽,一日三餐均為麵包,從早肝到晚,肝了5天,累到半死。也因此頹了一週
有的題會重要寫一下思路,大部分題題解應該會比較簡單不會贅述(有的跨時太久可能也忘了
如有不正確的地方還請不吝賜教,我會十分感謝

下面進入正題

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

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

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

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

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

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

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

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

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

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