Advertisement
XuanHong

LTTS - ứng dụng Fibonaci: số nhát cắt i với x mét sắt...

Feb 2nd, 2015
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1.  
  2. // LTTS - có 1 thanh sắt dài x mét, hỏi số nhát cắt sao cho khi yêu cầu y mét < = x ta đều đáp ứng được, biết phải cắt trước khi biết y
  3. // input x, output i (số nhát cắt)
  4.  
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. int f0=1;
  9. int f1=1;
  10. int f[1000];
  11. int x;
  12.  
  13. void main()
  14. {
  15.     cin>>x;
  16.     f[0]=f0;
  17.     f[1]=f1;
  18.     int k=0;
  19.     for(int i=2; i<1000; i++)
  20.     {
  21.         f[i]=f[i-1]+f[i-2];
  22.     }
  23.    
  24.     for(int i=0; i<1000; i++)
  25.     {
  26.         k+=f[i];
  27.         if(k>=x)
  28.         {
  29.             cout<<i<<endl;
  30.             return;
  31.         }
  32.        
  33.     }
  34.    
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement