poj2116——模拟

题目链接:http://poj.org/problem?id=2116

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、2、3、5,... -8,... -则由 F 定义0 = 1,F1 = 2 和递归关系 F ‎‎n‎‎ = F ‎‎n-1‎‎ = F ‎‎n-2,‎‎用于 n >= 2)。例如 1101001 Fib = F0 + F3 + F5 = F6 + 1 + 5 + 13 + 21 = 40。 ‎‎ ‎
‎ ‎

‎您可能会注意到,每个整数都可以在此基中表示,但不一定以独特的方式表示 - 例如,40 也可以表示为 10001001 ‎‎Fib‎‎ ‎
‎。但是,对于任何整数,都有一个不包含两个相邻数字 1 的唯一表示形式 - 我们称之为规范的表示形式。例如,10001001 ‎‎Fib‎‎是一个规范的斐波那契表示形式 40。‎
‎为了证明数字的这种表示优于其他数字,ACM 决定创建一台在斐波那契基地进行计算‎
‎的计算机。您的任务是创建一个程序,该程序在斐波那契基中获取两个数字(不一定是规范表示形式),并将它们相加。‎

‎输入‎

‎输入由多个实例组成,每个实例由一行组成。输入的每一行在斐波那契基包含两个数字 X 和 Y,由单个空格分隔。每个数字最多有 40 位数字。输入的末尾未以任何特殊方式标记。‎

‎输出‎

‎每个实例的输出的格式应如下所示:‎

‎第一行包含规范表示形式中的数字 X,可能由空格从左侧填充。第二行以加号开头,后跟规范表示形式中的数字 Y,可能由空格从左侧填充。第三行以两个空格开头,后跟一串与添加结果长度相同的减号。第四行以两个空格开头,紧接着是 X = Y 的规范表示形式。X 和 Y 都用空格从左侧填充,以便 X、Y 和 X = Y 的最小有效数字位于输出的同一列中。每个实例的输出后跟一个空行。‎

 

看着很复杂,其实就是模拟一下就行了,用string的reverse很方便,注意前导0的情况。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define ll long long
using namespace std;
ll F[50];
string s1,s2,s3;
void init(){
	F[0]=1,F[1]=2;
	for(int i=2;i<=45;i++)
		F[i]=F[i-1]+F[i-2];
}
ll getnum(string s){
	ll ans=0;
	int len=s.size();
	for(int i=0;i<len;++i)
		if(s[i]=='1')
			ans+=F[i];
	return ans;
}
void solve(ll n,string& s){
	int i;
	for(i=45;i>=0;i--)
		if(F[i]<=n)
			break;
	for(int t=i;t>=0;t--){
		if(n>=F[t]){
			n-=F[t];
			s+='1';
		}
		else 
		s+='0';
	}
	if(i<0) s+='0';
}
int main(){
	init();
	while(cin>>s1>>s2){
		reverse(s1.begin(),s1.end());
		reverse(s2.begin(),s2.end());
		ll a=getnum(s1);
		ll b=getnum(s2);
		ll sum=a+b;
		s1.clear();
		s2.clear();
		s3.clear();
		solve(a,s1);
		solve(b,s2);
		solve(sum,s3);
		int l1=s1.size(),l2=s2.size(),l3=s3.size();
		printf("  ");
		for(int i=0;i<l3-l1;i++)
			printf(" ");
		cout<<s1<<endl;
		printf("+ ");
		for(int i=0;i<l3-l2;i++)
			printf(" ");
		cout<<s2<<endl;
		printf("  ");
		for(int i=0;i<l3;++i)
			printf("-");
		cout<<endl;
		printf("  ");
		cout<<s3<<endl;
		cout<<endl;
	} 
	return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值