HDU 4814 golden radio base

Source: Internet
Author: User

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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.