• POJ


    The group of Absurd Calculation Maniacs has discovered a great new way how to count. Instead of using the ordinary decadic numbers, they use Fibonacci base numbers. Numbers in this base are expressed as sequences of zeros and ones similarly to the binary numbers, but the weights of bits (fits?) in the representation are not powers of two, but the elements of the Fibonacci progression (1, 2, 3, 5, 8,... - the progression is defined by F0 = 1, F1 = 2 and the recursive relation F n = F n-1 + F n-2 for n >= 2). 


    For example 1101001 Fib = F0 + F3 + F5 + F6 = 1 + 5 + 13 + 21 = 40. 


    You may observe that every integer can be expressed in this base, but not necessarily in a unique way - for example 40 can be also expressed as 10001001 Fib. However, for any integer there is a unique representation that does not contain two adjacent digits 1 - we call this representation canonical. For example 10001001 Fib is a canonical Fibonacci representation of 40. 


    To prove that this representation of numbers is superior to the others, ACM have decided to create a computer that will compute in Fibonacci base. Your task is to create a program that takes two numbers in Fibonacci base (not necessarily in the canonical representation) and adds them together. 
    Input
    The input consists of several instances, each of them consisting of a single line. Each line of the input contains two numbers X and Y in Fibonacci base separated by a single space. Each of the numbers has at most 40 digits. The end of input is not marked in any special way.
    Output
    The output for each instance should be formated as follows: 


    The first line contains the number X in the canonical representation, possibly padded from left by spaces. The second line starts with a plus sign followed by the number Y in the canonical representation, possibly padded from left by spaces. The third line starts by two spaces followed by a string of minus signs of the same length as the result of the addition. The fourth line starts by two spaces immediately followed by the canonical representation of X + Y. Both X and Y are padded from left by spaces so that the least significant digits of X, Y and X + Y are in the same column of the output. The output for each instance is followed by an empty line. 
    Sample Input
    11101 1101
    1 1
    Sample Output
       100101
    +   10001
      -------
      1001000


       1
    +  1
      --

      10


    题意:数字用类似二进制的表示方法而且1的个数尽量少

    #include<stack>
    #include<queue>
    #include<math.h>
    #include<vector>
    #include<string>
    #include<stdio.h>
    #include<map>
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    #define maxn 45
    #define maxm 10000005
    #define MAXN 100005
    #define MAXM 10005
    #define mem(a,b) memset(a,b,sizeof(a))
    #define ll long long
    #define inf 0x3f3f3f3f
    using namespace std;
    ll f[45];
    int a[45],lim;
    void init(){
       f[0]=1;f[1]=2;
       for(int i=2;i<=40;i++){
        f[i]=f[i-1]+f[i-2];
       }
    }
    vector<int> solve(ll n){
        for(int i=0;i<maxn;i++){
            if(f[i]>n){
                lim=i;break;}
        }
        vector<int> ret;
        for(int i=lim-1;i>=0;--i){
            if(f[i]<=n){
                ret.push_back(1);
                n-=f[i];
            }else ret.push_back(0);
        }
        return ret;
    }
    
    int main(){
        init();
        string s1,s2;
        while(cin>>s1>>s2){
         vector<int>a;
         vector<int>b;
         vector<int>c;
         ll x1=0,x2=0;
         for(int i=0;i<s1.size();i++)
            if(s1[i]=='1')x1+=f[s1.size()-i-1];
         for(int j=0;j<s2.size();j++)
            if(s2[j]=='1')x2+=f[s2.size()-j-1];
         a=solve(x1);b=solve(x2);c=solve(x1+x2);
         int a1=a.size(),b1=b.size(),c1=c.size();
         printf("  ");
            if(a1==0)a1=1;
            while(a1<c1){printf(" ");a1++;}
            for(int i=0;i<a.size();i++)
            cout<<a[i];
            if(a.size()==0)cout<<"0";
            cout<<endl;
        printf("+ ");
            if(b1==0)b1=1;
            while(b1<c1){printf(" ");b1++;}
            for(int i=0;i<b.size();i++)
            cout<<b[i];
            if(b.size()==0)cout<<"0";
            cout<<endl;
        printf("  ");
            if(c1==0)c1=1;
            while(c1--)printf("-");
            cout<<endl;
        printf("  ");
            for(int i=0;i<c.size();i++)
            cout<<c[i];
            if(c.size()==0)cout<<"0";
            cout<<endl;
        cout<<endl;
        }
    }
    

  • 相关阅读:
    ng2-bootstrap的modal嵌套时无法滚动的情况
    oracle自动补0
    webservice 从客户端中检测到有潜在危险的 Request.Form 值
    树莓派花生壳
    ubuntu E: Could not get lock /var/lib/dpkg/lock
    树莓派配置静态ip
    解决PL/SQL查询结果乱码的问题
    批处理脚本命令行方式关闭Windows服务
    最简单的分享到微博代码
    Select的onchange事件
  • 原文地址:https://www.cnblogs.com/da-mei/p/9053222.html
Copyright © 2020-2023  润新知