有隻可愛的蝸牛沒事做,想說來挑戰爬高高。
蝸牛開始位置在0,牠想爬上高度為100的樹;牠早上會往上爬5,晚上睡覺時會下滑2。
樹上有許多枝幹,如果牠剛好爬到有樹幹的地方,晚上就可以在樹幹上睡覺不會往下滑。
Input:樹幹所在高度,不定數量的數字,數字範圍可用int存取
Output:可愛蝸牛爬到樹梢所需的最短天數
檔案1--"Snall_Climb_Tree.h"
#include <iostream>
using namespace std;
class Snall_Climb_Tree
{
public:
Snall_Climb_Tree(int *brounch);
void ClimbDay(int n);
void Print_DayOfClimb();
private:
int *pri_brounch;
int pri_n;
int day;
int hight;
};
檔案2--"Snall_Climb_Tree.cpp":
#include "Snall_Climb_Tree.h"
Snall_Climb_Tree::Snall_Climb_Tree(int *brounch)
{
pri_brounch = brounch;
}
void Snall_Climb_Tree::ClimbDay(int n)
{
pri_n = n;
day=0;
hight=0;
for(;hight<100;)
{
/*morning*/
++day;
hight+=5;
if(hight>=100)
return;
/*night*/
int i;
bool SleepPlace=0;
for(i=0;i<n;i++)
if(hight==pri_brounch[i])
{
SleepPlace = 1;
break;
}
if(!SleepPlace)
hight-=2;
}
}
void Snall_Climb_Tree::Print_DayOfClimb()
{
cout<<day<<endl;
}
檔案3--實作main檔:
#include <iostream>
using namespace std;
#include "Snall_Climb_Tree.cpp"
int main()
{
int array[10000];
int n;
for(n=0;scanf("%d",&array[n])!=EOF;n++){/**if(n==24)break;**/}
Snall_Climb_Tree obj01(array);
obj01.ClimbDay(n+1);
obj01.Print_DayOfClimb();
return 0;
}
/**
Sample Input:2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
Sample output:32
**/