UVA 763 - Fibinary Numbers(高精度斐波那契)

Fibinary Numbers 

The standard interpretation of the binary number  1010  is 8 + 2 = 10. An alternate way to view the sequence `` 1010 '' is to use Fibonacci numbers as bases instead of powers of two. For this problem, the terms of the Fibonacci sequence are: 

\begin{displaymath}1, 2, 3, 5, 8, 13, 21 , \dots\end{displaymath}

Where each term is the sum of the two preceding terms (note that there is only one 1 in the sequence as defined here). Using this scheme, the sequence ``1010'' could be interpreted as $1 \cdot 5 + 0 \bullet 3 + 1 \bullet 2 + 0 \bullet 1 = 7$. This representation is called a Fibinary number.


Note that there is not always a unique Fibinary representation of every number. For example the number 10 could be represented as either 8 + 2 (10010) or as 5 + 3 + 2 (1110). To make the Fibinary representations unique, larger Fibonacci terms must always be used whenever possible (i.e. disallow 2 adjacent 1's). Applying this rule to the number 10, means that 10 would be represented as 8+2 (10010).

Input and Output 

Write a program that takes two valid Fibinary numbers and prints the sum in Fibinary form. These numbers will have at most 100 digits.

In case that two or more test cases had to be solved, it must be a blank line between two consecutive, both in input and output files.

Sample Input 

10010
1

10000
1000

10000
10000

Sample Output 

10100

100000

100100

题意:给2个斐波那契进制数。求他们的和,输出也要是斐波那契进制数,并且是不能有相邻的1的表示方法

思路:高精度。。自己写了个模版一直WA,最后拿了小伙伴的模版来用,一下就过了。思路很简单,就是进制转化相加了在转换回去

代码:

#include <stdio.h>
#include <string.h>
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
const int N = 105;
const int MAXBIGN = 305;


struct bign {
    int s[MAXBIGN];
    int len;
    bign() {
	len = 1;
	memset(s, 0, sizeof(s));
    }

    bign operator = (const char *number) {
	len = strlen(number);
	for (int i = 0; i < len; i++)
	    s[len - i - 1] = number[i] - '0';
	return *this;
    }
    bign operator = (const int num) {
	char number[N];
	sprintf(number, "%d", num);
	*this = number;
	return *this;
    }

    bign (int number) {*this = number;}
    bign (const char* number) {*this = number;}

    bign operator + (const bign &c){  
	bign sum;
	int t = 0;
	sum.len = max(this->len, c.len);
	for (int i = 0; i < sum.len; i++) {
	    if (i < this->len) t += this->s[i];
	    if (i < c.len) t += c.s[i];
	    sum.s[i] = t % 10;
	    t /= 10;
	}

	while (t) {
	    sum.s[sum.len++] = t % 10;
	    t /= 10;
	}

	return sum;  
    }

    bign operator - (const bign &c) {
	bign ans;
	ans.len = max(this->len, c.len);
	int i;

	for (i = 0; i < c.len; i++) {
	    if (this->s[i] < c.s[i]) {
		this->s[i] += 10;
		this->s[i + 1]--;
	    }
	    ans.s[i] = this->s[i] - c.s[i];
	}



	for (; i < this->len; i++) {
	    if (this->s[i] < 0) {
		this->s[i] += 10;
		this->s[i + 1]--;
	    }
	    ans.s[i] = this->s[i];
	}
	while (ans.s[ans.len - 1] == 0) {
	    ans.len--;
	}
	if (ans.len == 0) ans.len = 1;
	return ans;
    }

    void put() {
	if (len == 1 && s[0] == 0) {
	    printf("0");
	} else {
	    for (int i = len - 1; i >= 0; i--)
		printf("%d", s[i]);
	}
    }

    bool operator < (const bign& b) const {
	if (len != b.len)
	    return len < b.len;

	for (int i = len - 1; i >= 0; i--)
	    if (s[i] != b.s[i])
		return s[i] < b.s[i];
	return false;
    }
    bool operator > (const bign& b) const { return b < *this; }
    bool operator <= (const bign& b) const { return !(b < *this); }
    bool operator >= (const bign& b) const { return !(*this < b); }
    bool operator != (const bign& b) const { return b < *this || *this < b;}
    bool operator == (const bign& b) const { return !(b != *this); }
};
bign f[N];

void init() {
    f[0] = "1"; f[1] = "2";
    for (int i = 2; i < N; i ++)
	f[i] = f[i - 1] + f[i - 2];
}

char a[N], b[N];

bign change(char *a) {
    bign sum; int len = strlen(a);
    for (int i = 0; i < len; i ++) {
	if (a[len - 1 - i] == '1') {
	    sum = sum + f[i];
	}
    }
    return sum;
}
bign n3, zero;

void solve() {
    if (n3 == zero) {
	printf("0\n");
	return;
    }
    int i, flag = 1;
    for (i = N - 1; i >= 0; i --)
	if (n3 >= f[i]) break;
    for (;i >= 0; i --) {
	if (n3 >= f[i] && flag) {
	    n3 = n3 - f[i];
	    printf("1");
	    flag = 0;
	}
	else {
	    flag = 1;
	    printf("0");
	}
    }
    printf("\n");
}

int main () {
    int bo = 0; init();
    while (~scanf("%s%s", a, b)) {
	if (bo ++) printf("\n");
	n3 = change(a) + change(b);
	solve();
    }	
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值