• poj 2116 Death to Binary? 模拟


    Death to Binary?
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 1707   Accepted: 529

    Description

    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 Fn = Fn-1 + Fn-2 for n >= 2).

    For example 1101001Fib = 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 10001001Fib. However, for any integer there is a unique representation that does not contain two adjacent digits 1 - we call this representation canonical. For example 10001001Fib 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
    

    Source

    题意:给你一个两个字符串,一个字符串的值等于为1位置的斐波那契的和,比如1101001Fib = F0 + F3 + F5 + F6 = 1 + 5 + 13 + 21 = 40,一个值可能有多种不同的写法,需要改成没有相邻的1的写法, 写成加法的式子;
    思路:模拟,坑点  0 0;和前导0;
    #include<iostream>
    #include<string>
    #include<cstring>
    #include<algorithm>
    #include<cstdio>
    using namespace std;
    #define ll long long
    #define esp 1e-13
    const int N=1e4+10,M=1e6+50000,inf=1e9+10,mod=1000000007;
    string s1,s2,s3;
    ll a[N];
    void init()
    {
        a[0]=1;
        a[1]=2;
        for(int i=2;i<=50;i++)
        a[i]=a[i-1]+a[i-2];
    }
    ll getnum(string aa)
    {
        int x=aa.size();
        ll sum=0;
        for(int i=0;i<x;i++)
        if(aa[i]=='1')
        sum+=a[i];
        return sum;
    }
    void check(ll x,string &str)
    {
        int i;
        for(i=50;i>=0;i--)
        if(x>=a[i])
        break;
        for(int t=i;t>=0;t--)
        if(x>=a[t])
        {
            str+='1';
            x-=a[t];
        }
        else
        str+='0';
        if(i<0)
        str+='0';
    }
    int main()
    {
        int x,y,i,z,t;
        init();
        while(cin>>s1>>s2)
        {
    
            reverse(s1.begin(),s1.end());
            reverse(s2.begin(),s2.end());
            ll num1=getnum(s1);
            ll num2=getnum(s2);
            ll num3=num1+num2;
            s1.clear();
            s2.clear();
            s3.clear();
            check(num1,s1);
            check(num2,s2);
            check(num3,s3);
            printf("  ");for(i=0;i<s3.size()-s1.size();i++)printf(" ");cout<<s1<<endl;
            printf("+ ");for(i=0;i<s3.size()-s2.size();i++)printf(" ");cout<<s2<<endl;
            printf("  ");for(i=0;i<s3.size();i++)printf("-");cout<<endl;
            printf("  ");cout<<s3<<endl;
            cout<<endl;
        }
        return 0;
    }
  • 相关阅读:
    CollectionView网格布局
    UICollectionView基础/UICollectionViewCell的四种创建方式
    shiro
    jquery添加属性的方法
    ssm+activiti+maven
    Activiti流程定义部署方式
    00--工作流
    04--activiti demo
    02--Activiti初始化表
    01--安装Activiti流程设计器eclipse插件
  • 原文地址:https://www.cnblogs.com/jhz033/p/5765763.html
Copyright © 2020-2023  润新知