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

发错了代码,思路一样,但是递归的话就是超时

Posted by HH_YT at 2012-09-20 14:36:54 on Problem 1090
In Reply To:不用递推,只是大数问题 Posted by:HH_YT at 2012-09-20 14:36:00
#include<stdio.h>
#include<string.h>
int array[1001];
int res[1001],ans[1001];
int ini()
{
	int z,flag=0;
	for(z=1;z<=1000;z++)
	{
		res[z]+=flag;
		flag=res[z]/10;
		res[z]%=10;
	}
	return 0;
}
int add(int* flag,int fnum)
{
	int i;
	for(i=1;i<=fnum;i++)
		res[i]+=flag[i];
	return ini();
}
int getres(int n)
{
	int i,j,z,flag[1001],ff,fnum=1;
	memset(flag,0,sizeof(flag));
	flag[1]=1;
	for(i=1;i<=n;i++)
	{
		if(ans[i])
			add(flag,fnum);
		for(j=1;j<=fnum;j++)
		{
			flag[j]*=2;
		}
		ff=0;
		for(z=1;z<=fnum;z++)
		{
			flag[z]+=ff;
			ff=flag[z]/10;
			flag[z]%=10;
		}
		while(ff!=0)
		{
			flag[++fnum]=ff%10;
			ff/=10;
		}
	}
	return 0;
}
int main()
{
	int i;
	int n,flag[1001],xx;
	while(scanf("%d",&n)!=EOF)
	{
		memset(flag,0,sizeof(flag));
		memset(res,0,sizeof(res));
		memset(ans,0,sizeof(ans));
		for(i=1;i<=n;i++)
			scanf("%d",&array[i]);
		for(i=n;i>=1;i--)
		{
			if(flag[i]==array[i])
				continue;
			flag[i-1]=1;
			ans[i]=1;
		}
		getres(n);
		xx=0;
		for(i=1000;i>=1;i--)
		{
			if(xx==0&&res[i]==0)
				continue;
			xx=1;
			printf("%d",res[i]);
		}
		if(xx==0)
			printf("0");
		printf("\n");
	}
	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