Question:
Convert a 10-digit number to X-digit number x = (1 + √ 5)/2
Ideas:
The game was scared by the logic of the number. Actually, it's not necessary because n = N * 1 = N * x ^ 0.
Since the zero power of X is equal to 1, we can regard N as a single digit of the corresponding X-base number. Now we need to convert this number into binary.
The formula given by the question can multiply the power of X at the same time on both sides. Then the two formulas become
X ^ (I + 1) + x ^ I = x ^ (I + 2)
2 * x ^ I = x ^ (I + 1) + x ^ (I-2)
Use these two formulas to constantly change the number until the number does not change.
Code:
#include<cstdio>#include<cstring>#include<algorithm>using namespace std;#define N 100int a[N*2];int n,u,v;int main(){ int i,k,flag; while(~scanf("%d",&n)) { memset(a,0,sizeof(a)); a[N]=n; do { flag=0; for(i=0;i<N*2-2;i++) { if(a[i]&&a[i+1]) { k=min(a[i],a[i+1]); a[i]-=k; a[i+1]-=k; a[i+2]+=k; flag=1; } } for(i=2;i<N*2-1;i++) { if(a[i]>1) { k=a[i]/2; a[i]%=2; a[i-2]+=k; a[i+1]+=k; flag=1; } } }while(flag); for(u=2*N-1;u>N&&!a[u];u--); for(v=0;v<N&&!a[v];v++); for(i=u;i>=N;i--) printf("%d",a[i]); if(v!=N) { printf("."); for(i=N-1;i>=v;i--) printf("%d",a[i]); } printf("\n"); } return 0;}