C++編程作業: 綜合練習(1)

來源: POJ

編程題#1:年齡與疾病

注意: 總時間限制: 1000ms 內存限制: 65536kB

1.1 描述

某醫院想統計一下某項疾病的獲得與否與年齡是否有關,需要對以前的診斷記錄進行整理。

輸入
共2行,第一行爲過往病人的數目n(0 < n <= 100),第二行爲每個病人患病時的年齡。

輸出
每個年齡段(分四段:18以下,19-35,36-60,大於60注意看樣例輸出的格式)的患病人數佔總患病人數的比例,以百分比的形式輸出,精確到小數點後兩位(double)。關於c++的格式化的輸入輸出,請參考:http://www.cplusplus.com/reference/iomanip。也可以在網上搜索一下,資料很多的。

樣例輸入

10
1 11 21 31 41 51 61 71 81 91

樣例輸出

1-18: 20.00%
19-35: 20.00%
36-60: 20.00%
60-: 40.00%

1.2 提示

注意最後一行的輸出是“60-: ”,而不是“61-: ”。
每個冒號之後有一個空格。

輸出可以用 cout<<fixed<<setprecision(2) << f; 來保留f後面的兩位小數。

1.3 解答

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    int n, score, tmp;
    double per[4] = {0};
    cin >> n;
    tmp = n;
    while (tmp--) {
        cin >> score;
        if (score > 0 && score <= 100) {
            if (score <= 18)
                per[0]++;
            else if (score <= 35)
                per[1]++;
            else if (score < 60)
                per[2]++;
            else if (score == 60) {
                per[2]++;
                per[3]++;
            } else
                per[3]++;
        }
    }
    for (int i = 0; i < 4; i++)
        per[i] = per[i] / n;
    cout << "1-18: " << fixed << setprecision(2) << per[0] * 100 << "%" << endl;
    cout << "19-35: " << fixed << setprecision(2) << per[1] * 100 << "%" << endl;
    cout << "36-60: " << fixed << setprecision(2) << per[2] * 100 << "%" << endl;
    cout << "60-: " << fixed << setprecision(2) << per[3] * 100 << "%" << endl;
}

編程題#2:成績判斷

注意: 總時間限制: 1000ms 內存限制: 6000kB

2.1 描述

輸入一個0–100的分數,判斷分數代表什麼等級。

95<=分數<=100, 輸出1
90<=分數<95,輸出2
85<=分數<90,輸出3
80<=分數<85,輸出4
70<=分數<80,輸出5
60<=分數<70輸出6
分數 < 60;輸出7.

輸入
n

輸出
m

樣例輸入

87

樣例輸出

3

2.2 解答

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    int n, m;
    cin >> n;
    if (n >= 0 && n < 100){
        if (n >= 95)
            m = 1;
        else if (n >= 90 && n < 95)
            m = 2;
        else if (n >= 85 && n < 90)
            m = 3;
        else if (n >= 80 && n < 85)
            m = 4;
        else if (n >= 70 && n < 80)
            m = 5;
        else if (n >= 60 && n < 70)
            m = 6;
        else
            m = 7;
    }
    cout << m <<endl;
}

編程題#3:找出第k大的數

注意: 總時間限制: 1000ms 內存限制: 65536kB

3.1 描述

用戶輸入N和K,然後接着輸入N個正整數(無序的),程序在不對N個整數排序的情況下,找出第K大的數。注意,第K大的數意味着從大到小排在第K位的數。

輸入
N
K
a1 a2 a3 a4 … aN

輸出
b

樣例輸入

5
2
32 3 12 5 89

樣例輸出

32

3.2 解答

設定b很大(大於每一個輸入的a[n]),並設定一個tmp很小(小於每一個輸入的a[n])。
然後重複K次以下動作:找出大於tmp且小於b的值(也就是第m大的數,m代表執行次數),然後把這個數賦給b。
最後得到的就是第K大的數了。全過程沒有涉及排序。

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    int a[999], b = 9999, N, K, *pa, tmp = 0;
    cin >> N;
    cin >> K;
    pa = a;
    for (int i = N; i > 0; i--) {
        cin >> *pa;
        pa++;
    }
    pa = a;
    while (K--) {
        for (int i = N; i > 0; i--) {
            if (*pa > tmp && *pa < b)
                tmp = *pa;
            pa++;
        }
        b = tmp;
        tmp = 0;
        pa = a;
    }
    cout << b;
}

編程題#4:人民幣支付

注意: 總時間限制: 1000ms 內存限制: 65536kB

4.1 描述

從鍵盤輸入一指定金額(以元爲單位,如345),然後輸出支付該金額的各種面額的人民幣數量,顯示100元,50元,20元,10元,5元,1元各多少張,要求儘量使用大面額的鈔票。

輸入
一個小於1000的正整數。

輸出
輸出分行,每行顯示一個整數,從上到下分別表示100元,50元,20元,10元,5元,1元人民幣的張數

樣例輸入

735

樣例輸出

7
0
1
1
1
0

4.2 解答

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    int money, hundred, fifty, twenty, ten, five, one, remain;
    cin >> money;
    if (money < 1000) {
        hundred = (money - money % 100) / 100;
        remain = money - hundred * 100;
        fifty = (remain - remain % 50) / 50;
        remain -= fifty * 50;
        twenty = (remain - remain % 20) / 20;
        remain -= twenty * 20;
        ten = (remain - remain % 10) / 10;
        remain -= ten * 10;
        five = (remain - remain % 5) / 5;
        one = remain - five * 5;
    }
    cout << hundred << endl << fifty << endl << twenty << endl << ten << endl << five << endl << one << endl;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章