每个正整数都可以分解成斐波那契数列中的几个数相加……
从大到小贪心法就可以了……
#include <iostream> #include <cstdio> #include <fstream> #include <algorithm> #include <cmath> #include <deque> #include <vector> #include <queue> #include <string> #include <cstring> #include <map> #include <stack> #include <set> #define PI acos(-1.0) #define mem(a,b) memset(a,b,sizeof(a)) #define sca(a) scanf("%d",&a) #define sc(a,b) scanf("%d%d",&a,&b) #define pri(a) printf("%d\n",a) #define lson i<<1,l,mid #define rson i<<1|1,mid+1,r #define MM 1000005 #define MN 2005 #define INF 100004 #define eps 1e-7 using namespace std; typedef long long ll; string solve(int n) //n的斐波那契进制转换 { static int fib[305]; fib[0]=fib[1]=1; int i,j; for(i=2;i<300;i++) { fib[i]=fib[i-1]+fib[i-2]; //斐波那契数列 if(fib[i]>n) {j=i;break;} } string s; for(i=j-1;i>0;i--) if(fib[i]<=n) s+=‘1‘,n-=fib[i]; else s+=‘0‘; return s; } int main() { int t; sca(t); while(t--) { int n; sca(n); string s=solve(n); cout<<n<<" = "<<s<<" (fib)"<<endl; } return 0; }
UVA 948 数的斐波那契进制表示,码迷,mamicode.com
原文地址:http://blog.csdn.net/u011466175/article/details/24601297