1. 程式人生 > >coursera 中計算概論的第五週 B-04作業

coursera 中計算概論的第五週 B-04作業

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

輸入
共2行,第一行為過往病人的數目n(0 < n <= 100),第二行為每個病人患病時的年齡。
輸出
每個年齡段(分四段:1-18,19-35,36-60,61-注意看樣例輸出的格式)的患病人數佔總患病人數的比例,以百分比的形式輸出,精確到小數點後兩位(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%
提示
C++裡輸出可以在include iomanip檔案之後用下面的語句
cout << fixed;
cout << setprecision(2) << x << '%' << endl;*/
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int n;//n為過往病人的數目
    cin >> n;
    int stage1=0,stage2=0,stage3=0,stage4=0;
    for(int i=0;i<n;i++)
    {
        int temp ;//temp儲存輸入的病人的年齡
        cin >> temp ;
        if(temp<=18)
            stage1++;
        else if(temp<=35)
            stage2++;
        else if(temp<=60)
            stage3++;
        else if(temp<=100)
            stage4++;
        else
            cout << temp << "error!";
    }
    double sum=(stage1+stage2+stage3+stage4)/100.0;
    //為避免整數除法向下取整,總人數選取double型
    cout << fixed << setprecision(2)
         << "1-18:  " << stage1/sum << "%" << endl
         << "19-35: " << stage2/sum << "%" << endl
         << "36-60: " << stage3/sum << "%" << endl
         << "60-:   " << stage4/sum << "%" << endl;

    return 0;
}
/*
描述
輸入一個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
*/
#include <iostream>

using namespace std;

int main()
{
    int record;//0<=record<=100
    while(cin >> record)
    {
        if(record>=95&&record<=100)
            cout << 1 << endl;
        else if(record>=90)
            cout << 2 << endl;
        else if(record>=85)
            cout << 3 << endl;
        else if(record>=80)
            cout << 4 << endl;
        else if(record>=70)
            cout << 5 << endl;
        else if(record>=60)
            cout << 6 << endl;
        else if(record<60)
            cout << 7 << endl;
        else
            cout << "this record is error!" << endl;
    }
    return 0;
}

/*
描述
使用者輸入N和K,然後接著輸入N個正整數(無序的),程式在不對N個整數排序的情況下,
找出第K大的數。注意,第K大的數意味著從大到小排在第K位的數。

輸入
N
K
a1 a2 a3 a4 ..... aN
輸出
b
樣例輸入
5
2
32 3 12 5 89
樣例輸出
32
*/
//對每個數字,挨個比較找出比他大的數的個數,如果剛好是k-1個,他就是第k大
#include <iostream>

using namespace std;

int main()
{
    int n=0,k;//n為資料的個數,求第k大的數
    cin >> n >> k;
    int a[n];
    for(int i=0;i<n;i++)//錄入資料
        cin >> a[i];
    //遍歷整個陣列
    for(int i=0;i<n;i++)
    {
        int num=0;//記錄有幾個數比所選的這個數大
        for(int j=0;j<n;j++)
        {
            if(a[i]<a[j])
                num++;
        }
        if(num==k-1)
            cout << a[i] << endl;
        return 0;
    }
    return 0;
}

/*
描述
2008年北京奧運會,A國的運動員參與了n天的決賽專案(1≤n≤17)。
現在要統計一下A國所獲得的金、銀、銅牌數目及總獎牌數。

輸入
第1行是A國參與決賽專案的天數n,其後有n行,每一行是該國獲
得的金、銀、銅牌數目,用空格隔開。
輸出
1行,包括4個整數,為A國所獲得的金、銀、銅牌總數及總獎牌數
,用空格隔開。
樣例輸入
3
1 0 3
3 1 0
0 3 0
樣例輸出
4 4 3 11
*/
#include <iostream>

using namespace std;

int main()
{
    int n;//n為比賽天數
    cin >> n;
    int gold=0,silver=0,bronze=0;
    for(int i=0;i<n;i++)
    {
        int temgold=0,temsilver=0,tembronze=0;
        cin >> temgold >> temsilver >> tembronze;
        gold +=temgold;
        silver +=temsilver;
        bronze +=tembronze;
    }
    cout << gold << " "
         << silver << " "
         << bronze << " "
         << gold+silver+bronze << " "<< endl;
    return 0;
}

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

輸入
一個小於1000的正整數。
輸出
輸出分行,每行顯示一個整數,從上到下分別表示100元,50元,20元,10元,5元,1元人民幣的張數
樣例輸入
735
樣例輸出
7
0
1
1
1
0*/
#include <iostream>

using namespace std;

int main()
{
    int money;//輸入處理的錢數
    cin >> money ;
    int deno[]={100,50,20,10,5,1};
    for(int i=0;i<6;i++)
    {
        cout << money/deno[i] << endl;
        money = money%deno[i];
    }
    return 0;
}