poj 2116 Death to Binary

传送门:点击打开链接

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 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.本题输入数据有0 0

2.本题输入数据可能会有前导0

3.在将输入数据处理成canonical representation时,需要多次循环,比如这个数10110,第一次循环会形成11000,并非完全体

4.注意输出格式

5.注意每组输出都有一个空行

说到底,就是一道十分易错的思维水题,考察了对格式的处理等编程基本技能。以上五组易错点要牢记,毕竟谁也不知道比赛会出什么易错点刁难我们

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<vector>
#include<set>
using namespace std;
typedef long long ll;
//433494435
const int N = 1000000 + 5;
ll rec[45];
char stra[45], strb[45], afa[45], afb[45], res[45];
void init()
{
    ll a = 1, b = 2;
    rec[0] = a; rec[1] = b;
    for(int i = 2; i < 45; i++)
    {
        ll c = a + b;
        a = b;
        b = c;
        rec[i] = b;
    }
}
int sear(ll tmp)
{
    int beg = 0, en = 44, mid;
    while(beg <= en)
    {
        mid = (beg + en) / 2;
        if(rec[mid] < tmp)
            beg = mid + 1;
        else if(rec[mid] > tmp)
            en = mid - 1;
        else
            return mid;
    }
    return en;
}
int main()
{
    init();
    while(~scanf("%s%s", stra, strb))
    {
        int cnta = 0, cntb = 0;
        int lena = strlen(stra), lenb = strlen(strb);
        for(int i = 0; i < lena; i++)
        {
            if(stra[i] == '0')
                cnta++;
            else
                break;
        }
        for(int i = 0; i < lenb; i++)
        {
            if(strb[i] == '0')
                cntb++;
            else break;
        }
        if(cnta == lena)
            cnta = lena - 1;
        if(cntb == lenb)
            cntb = lenb - 1;
        memset(afa, '0', sizeof(afa));
        memset(afb, '0', sizeof(afb));
        for(int i = 0, j = lena - 1; i < lena - cnta; i++, j--)
            afa[i] = stra[j];
        lena -= cnta;
        for(int i = 0, j = lenb - 1; i < lenb - cntb; i++, j--)
            afb[i] = strb[j];
        lenb -= cntb;
        bool ju = true;
        while(1)
        {
            for(int i = lena - 2; i >= 0; i--)
            {
                if(afa[i] == '1' && afa[i + 1] == '1')
                {
                    ju = false;
                    afa[i] = '0';
                    afa[i + 1] = '0';
                    afa[i + 2] = '1';
                    if(i == lena - 2)
                        lena++;
                }
            }
            if(ju)
                break;
            ju = true;
        }
        ju = true;
        while(1)
        {
            for(int i = lenb - 2; i >= 0; i--)
            {
                if(afb[i] == '1' && afb[i + 1] == '1')
                {
                    ju = false;
                    afb[i] = '0';
                    afb[i + 1] = '0';
                    afb[i + 2] = '1';
                    if(i == lenb - 2)
                        lenb++;
                }
            }
            if(ju)
                break;
            ju = true;
        }

        ll numa = 0, numb = 0;
        for(int i = 0; i < lena; i++)
        {
            if(afa[i] == '1')
                numa += rec[i];
        }
        for(int i = 0; i < lenb; i++)
        {
            if(afb[i] == '1')
                numb += rec[i];
        }
        ll num = numa + numb, numcopy = num;
        int len;
        memset(res, '0', sizeof(res));
        while(1)
        {
            int pos = sear(num);
            if(num == numcopy)
                len = pos + 1;
            num -= rec[pos];
            res[pos] = '1';
            if(num == 0)
                break;
        }
        if(len == 0)
            len = 1;
        int spacea = len - lena + 2, spaceb = lena - lenb + spacea - 1;
        for(int i = 0; i < spacea; i++)
            printf(" ");
        for(int i = lena - 1; i >= 0; i--)
            printf("%c", afa[i]);
        printf("\n+");
        for(int i = 0; i < spaceb; i++)
            printf(" ");
        for(int i = lenb - 1; i >= 0; i--)
            printf("%c", afb[i]);
        printf("\n  ");
        for(int i = 0; i < len; i++)
            printf("-");
        printf("\n  ");
        for(int i = len - 1; i >= 0; i--)
            printf("%c", res[i]);
        printf("\n\n");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值