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
#include
#includeusing 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
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;i--) printf("%d",a[i]); if(v!=N) { printf("."); for(i=N-1;i>=v;i--) printf("%d",a[i]); } printf("\n"); } return 0;}