題目敘述:又是這隻小小蝸牛,看到大樹就充滿決心想往上爬,位置在0開始,大樹高度100。

蝸牛天亮就爬5、天黑就會睡覺且會滑落2,但是蝸牛爬到的位置若有樹幹就可以在樹幹上睡覺不滑落。

Input:給定數列,數字代表樹幹位置;當中有不合法的數字(大於100或小於0),請用throw-try-catch抓出另外處理,輸出「wrong input」。

Output:輸出小蝸牛爬到樹頂(位置100)的最短天數

 

#include <iostream>
#include <memory.h>
#include <stdexcept>
using namespace std;

class tree
{
public:
    tree(int *input_brounch,int cnt,int input_snall_position)
    {
        tmp_brounch = input_brounch;
        tmp_cnt = cnt;
        snall = input_snall_position;
        memset(brounch,0,sizeof(brounch));
    }

    void CleanException(int i)
    {
        if(tmp_brounch[i]<0||tmp_brounch[i]>100)
        {
            throw invalid_argument{
            "wrong input"
            };

        }
        else{
            brounch[tmp_brounch[i]]=1;
        }
    }

    int SleepPosition(int position)
    {
        if(brounch[position])
            return 1;
        else
            return 0;
    }

    void DayOfClimb()
    {
        day=0,tree_hight=100;
        for(;snall<tree_hight;)
        {
            /**morning**/
            day++;
            snall+=5;
            /**night**/
            if(!SleepPosition(snall))
            {
                snall-=2;
            }
        }

        cout<<day<<endl;
    }
private:
    int brounch[101],*tmp_brounch;
    int tmp_cnt;
    int snall;
    int day;
    int tree_hight;
};

int main()
{
    int array[1000],i;
    for(i=0;cin>>array[i];i++){}

    tree obj01(array,i,0);
    int j;
    for(j=0;j<i;j++)
    {

        try
        {
            obj01.CleanException(j);
        }
        catch(invalid_argument &e)
        {
            cout<<e.what()<<endl;
        }
    }
    obj01.DayOfClimb();

    return 0;
}
 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Kuihao 的頭像
    Kuihao

    溫暖午後的金針田__孕育有趣的創新

    Kuihao 發表在 痞客邦 留言(0) 人氣()