Triangle HDU - 5914 (斐波那契數列的應用1123)

Mr. Frog has n sticks, whose lengths are 1,2, 3n respectively. Wallice is a bad man, so he does not want Mr. Frog to form a triangle with three of the sticks here. He decides to steal some sticks! Output the minimal number of sticks he should steal so that Mr. Frog cannot form a triangle with 
any three of the remaining sticks.
InputThe first line contains only one integer T (T20T≤20), which indicates the number of test cases. 

For each test case, there is only one line describing the given integer n (1n201≤n≤20). OutputFor each test case, output one line “Case #x: y”, where x is the case number (starting from 1), y is the minimal number of sticks Wallice should steal. Sample Input
3
4
5
6
Sample Output
Case #1: 1
Case #2: 1
Case #3: 2

剛開始看有點蒙,後來猜了幾組數據,看着眼熟,於是搞出來了;

上代碼;

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int  T;
int n;
int vis[20];
int main()
{
   scanf("%d", &T);
   vis[1] = vis[0] = 1;
   for(int i=2; i<=20; i++)
   {
	  vis[i] = vis[i-1] + vis[i-2];//構建斐波那契
   }
   for(int w=1; w<=T; w++)
   {
	 scanf("%d", &n);
     printf("Case #%d: ",w);
     for(int i=1; i<=20; i++)
     {
			if(vis[i] > n){cout << n-i+1 << endl;break;}
	 }
     
	}
     
	return 0;
}




水波.



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章