Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

这个程序有错?我考虑了Fibonacci数列的性质 ,即 11+11 = 1001 可是还是不对。。。。

Posted by sicheng at 2006-08-19 00:11:50 on Problem 2116
#include <iostream>
#include <string.h>

int a[45], b[45], c[45], i, j, al, bl, cl,f[46];
char t1[45], t2[45];

void tran(int * a, int &l)
{
	int i;
	while(a[l--]==0 && l>=1);    //消首位的0
	l++;
	for(i = l; i>=2; i--)
		if(a[i]==1 && a[i-1]==1)  //对adjacent 1做转化
		{
			a[i] = 0; 
			a[i-1] =0;
			a[i+1] = 1;
		}
	if(a[l+1] == 1)	l++; 
}

void plus(int * a, int * b)
{
	int i, adec = 0, bdec = 0, cdec = 0;
	f[1]=1;
	f[2]=2;
	for(i=3; i<=45; i++)
		f[i] = f[i-2] + f[i-1]; //确定数列的值
	for(i=1; i<=al; i++)        //得到a,b,c的十进制值
		adec += f[i]*a[i];
	for(i=1; i<=bl; i++)
		bdec += f[i]*b[i];
	cdec = adec + bdec;
	i=1;                         //得到c的位数
	while(cdec>=f[i++]);  i -= 2;
	if(i==0) i = 1;
	cl = i;                     
	for(i=cl; i>=1; i--)         //计算 c in the Fibonacci way
		if(cdec>=f[i]) cdec-=f[i], c[i] = 1;
}

void print()
{
	int i;
	for(i=1; i<=cl-al+2; i++) printf(" ");
	for(i=al; i>=1; i--) printf("%d", a[i]);
	printf("\n+");
	for(i=1; i<=cl-bl+1; i++) printf(" ");
	for(i=bl; i>=1; i--) printf("%d", b[i]);
	printf("\n  ");
	for(i=1; i<=cl; i++) printf("-");
	printf("\n  ");
	for(i=cl; i>=1; i--) printf("%d", c[i]);
	printf("\n\n");
}

int main()
{
	while(scanf("%s", t1)!=EOF)
	{
		scanf("%s", t2);
		memset(a, 0, sizeof(a));
		memset(b, 0, sizeof(b));
		memset(c, 0, sizeof(c));
		al = strlen(t1), bl = strlen(t2);
		for(i=0; i<al; i++)            //a,b中放的是倒序
			a[al-i] = t1[i]-'0';
		for(i=0; i<bl; i++)
			b[bl-i] = t2[i]-'0';
		plus(a, b);
		tran(a, al);
		tran(b, bl);
		tran(c, cl);
		print();
	}
	return 0;
}


			

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator