Golden Radio Base (模拟+推公式)

Golden ratio base (GRB) is a non-integer positional numeral system that uses the golden ratio (the irrational number (1+√5)/2 ≈ 1.61803399 symbolized by the Greek letter φ) as its base. It is sometimes referred to as base-φ, golden mean base, phi-base, or, phi-nary.

Any non-negative real number can be represented as a base-φ numeral using only the digits 0 and 1, and avoiding the digit sequence "11" – this is called a standard form. A base-φ numeral that includes the digit sequence "11" can always be rewritten in standard form, using the algebraic properties of the base φ — most notably that φ + 1 = φ 2 . For instance, 11(φ) = 100(φ). Despite using an irrational number base, when using standard form, all on-negative integers have a unique representation as a terminating (finite) base-φ expansion. The set of numbers which possess a finite base-φ representation is the ring Z[1 + √5/2]; it plays the same role in this numeral systems as dyadic rationals play in binary numbers, providing a possibility to multiply.

Other numbers have standard representations in base-φ, with rational numbers having recurring representations. These representations are unique, except that numbers (mentioned above) with a terminating expansion also have a non-terminating expansion, as they do in base-10; for example, 1=0.99999….

Coach MMM, an Computer Science Professor who is also addicted to Mathematics, is extremely interested in GRB and now ask you for help to write a converter which, given an integer N in base-10, outputs its corresponding form in base-φ.

Input

There are multiple test cases. Each line of the input consists of one positive integer which is not larger than 10^9. The number of test cases is less than 10000. Input is terminated by end-of-file.

Output

For each test case, output the required answer in a single line. Note that trailing 0s after the decimal point should be wiped. Please see the samples for more details.

Sample Input

1
2
3
6
10

Sample Output

1
10.01
100.01
1010.0001
10100.0101

        
  

Hint

         
  

题目中给出两个公式,手推一下前几项,可以发现两个规律:

φ^n+φ^(n+1)=φ^(n+2)

2*(φ^n)=φ^(n+1)+φ^(n-2)

然后一直模拟就好了

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+50;
int a[N];
int main()
{
    long long n;
    while(scanf("%lld",&n)!=EOF)
    {
        memset(a,0,sizeof(a));
        a[50]=n;///把原数放到某一位,两侧留出足够空间, 因为会出现小数部分
        bool flag=1;///用于标记是否还需循环
        while(flag)
        {
            flag=0;
            for(int i=0;i<100;i++)
            {
                if(a[i]&&a[i+1])
                {
                    int minn=min(a[i],a[i+1]);///一次性进位完,取二者中的较小值,加到i+2位
                    a[i+2]+=minn;
                    a[i]-=minn;
                    a[i+1]-=minn;
                    flag=1;///现在进行了这一步操作,说明接下来还有可能需要继续进位或拆分到两项里,再循环
                }
            }
            for(int i=2;i<100;i++)
            {
                if(a[i]>1)
                {
                    int x=a[i]/2;///一次性拆分完,求a[i]可以拆到a[i+1] a[i-2]里几个
                    a[i]%=2;
                    a[i+1]+=x;
                    a[i-2]+=x;
                    flag=1;///还需循环
                }
            }
        }
        int st,ed;///找到起始位置与终止位置
        for(int i=100;i>=0;i--)
        {
            if(a[i])
            {
                st=i;
                break;
            }
        }
        for(int i=0;i<100;i++)
        {
            if(a[i])
            {
                ed=i;
                break;
            }
        }
        for(int i=st;i>=ed;i--)
        {
            if(i==49)///输出小数点
                cout<<".";
            cout<<a[i];
        }
        cout<<'\n';
    }
    return 0;
}

 

全部评论

相关推荐

10-19 10:28
已编辑
西南石油大学 后端工程师
团孝子已上线feeling:面了很多家公司,能感受到目前只有小公司+外包喜欢问八股。大厂虽然也问八股,但是是从实习、项目中进行提问,并且大厂会问很深,面试官也会对你的回答进行思考➕追问,所以准备大厂面试前一定要备好相关资料。对于算法,我做的是codetop前100+力扣hot100+力扣高频150,面试中实感hot100就足够,基本上只要是hot100就秒答。对于项目和八股,我做的也是烂大街的星球项目,八股则是看小林和问ai,自己也写了很多技术博客和画了很多思维导图,并且自己也尝试用嘴巴说出来,不只停留于纸面。运气也很重要,必须要让面试官/HR看到简历才行,所以建议投递时间是下午两点。tl:第一岗位9.9&nbsp;投递9.10&nbsp;一面(一面评价:最近见过最强的大三,结束五分钟后约二面,都晚上九点了不下班吗)9.11&nbsp;二面(三道算法a出两道,反问评价:经验不够等横向,我实习生要啥经验)9.21挂(实习时间过短+其他原因,想要一年实习的,为什么不招个正职)第二岗位10.10投递10.11约面(主管打电话,说看到我之前投递记录了想要我挂qa职进去干后端,同意)10.14&nbsp;一面(无八股,主动说确实很强,意愿很强)10.16&nbsp;oc其余,友邦,东软,东华,惠择,用友oc已拒京东测开一面挂(投后端被测开捞)腾讯测试已拒(投后端被测开捞)ps:表扬惠择的主管面,没怎么问技术(可能是一面面试官沟通过了),全程一起讲大道理,解答了心中很多疑惑,也告诉我以面试官角度来看怎么选候选人,如果可以下次一定选惠择
HeaoDng:美团好像可以触发一面通
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务