Advertisement
XuanHong

LTTS - biễu diễn 1 số bằng tổng các số Fibonaci...

Feb 2nd, 2015
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. // LTTS - biễu diễn 1 số bằng tổng các số Fibonaci sao cho số chữ số Fibonaci xuất hiện là ít nhất
  2.  
  3. #include <iostream>
  4. using namespace std;
  5.  
  6. int f0=1;
  7. int f1=1;
  8. int f[1000];
  9. int n;
  10. int temp[100] = {0};
  11.  
  12. void main()
  13. {
  14.     cin>>n;
  15.     f[0]=f0;
  16.     f[1]=f1;
  17.     int k=0;
  18.     for(int i=2; i<1000; i++)
  19.     {
  20.         f[i]=f[i-1]+f[i-2];
  21.     }
  22.     int w = n;
  23.     for(int i=0; i<1000; i++)
  24.     {
  25.         if(f[i]==n)
  26.         {
  27.             cout<<f[i]<<endl;
  28.             return;
  29.         }
  30.         if(f[i]>n)
  31.         {
  32.             k= i-1;
  33.             cout<<f[k]<<endl;
  34.             break;
  35.         }
  36.     }
  37.     w-=f[k];
  38.     for(int i = k-1; i>=0; i--)
  39.     {
  40.         if(f[i]==w)
  41.         {
  42.             cout<<f[i]<<endl;
  43.             return;
  44.         }
  45.         if(f[i]<w)
  46.         {
  47.             cout<<f[i]<<endl;
  48.             w-=f[i];
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement