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

Re:我已经头晕分不清方向了

Posted by springtty at 2005-07-06 13:01:26 on Problem 1001
In Reply To:我已经头晕分不清方向了 Posted by:springtty at 2005-07-06 01:08:40
更新后的程序,研究过这题的人给几组BT的数据试试,看看哪里不对

#include <stdio.h>
#include <string.h>



int MaxLen ;
int Len1;
int IsDec;

void Reserve(char *R)
{
	char t;
	int i,j;
	i = 0;
	j = strlen(R) - 1;
	while(i<j)
	{
		t = R[i];
		R[i] = R[j];
		R[j] = t;
		i++;
		j--;
	}
}

void Multiply(char R[][160])
{
	int i = 0,j = 0;
	int n = 0;
	char inc = 0;
	int sum = 0;
  
	for(i = 0;i < Len1;i++)
		for(j = 0;j < MaxLen;j++)
		{
			R[i+1][j+i] = R[0][i]*R[6][j];		
		}
	
	MaxLen +=  Len1 -1;
	n = MaxLen;
	inc = 0;

	for(j=0;j<=n;j++)
	{
		sum = 0;

		for(i=1;i <= Len1;i++)
		{
			sum += R[i][j];			
		}
		
		sum += inc;
		
		inc  = sum/10;

		R[6][j] = sum % 10;

	}
		if(R[6][j-1])	MaxLen++;
		
}

void trim(char *R)
{
	int i = 0,j = strlen(R)-1;
	int k;
	char Buff[6];
	memset(Buff,0,sizeof(Buff));

	while(R[i] == '0')	i++;
	while(R[j] == '0')	j--;
	k = 0;
	for(;i!=j+1;i++)
		Buff[k++] = R[i];
	Buff[k] = 0;
	memcpy(R,Buff,sizeof(Buff));

}

void Process(char R[][160],int *Dec,int n)
{
	int i = 0;
	int j = 0;
	int Len;
	int withdec = 0;
	int size = 0;
	*Dec = 0;

	trim(R[0]);

	Len = strlen(R[0]);

	for(i = 0;i < Len;i++)
		if(R[0][i] == '.')	
		{
			break;
			withdec = 1;
		}

	
	
		*Dec =Len - i - 1;/*Get decimal*/	
		Len -= 1;
		for(i = Len - *Dec ;i < Len+1;i++)
			R[0][i] = R[0][i+1];/*shift left*/	
		R[0][i] = 0;
		
		*Dec = *Dec*n;
	
		Reserve(R[0]);
		MaxLen = Len;
		Len1 = Len;

		for(i = 0 ;i < Len;i++)/*copy*/
		{
			R[0][i] -= '0';
			R[6][i] = R[0][i];
		}



	for(i = 1;i < n;i++)
	{
		Multiply(R);

		for(j = 1;j <= 5;j++)/*clear memory*/			
			memset(R[j],0,sizeof(R[j]));				
	}

}

void Print(char R[][160],int Dec,int n)
{
	int i,j,k;
	char Buff[6];
	
	if(n == 1)	
	{	
	
	memset(Buff,0,sizeof(Buff));
	i = 0;
	j = 5;
	while(R[0][i] == '0')	i++;
	
	k = 0;
	for(;i!=j+1;i++)
		Buff[k++] = R[0][i];
	Buff[k] = 0;
	memcpy(R,Buff,sizeof(Buff));
	printf("%s\n",R[0]);

	}
	else if(IsDec)
	{
		i = Dec;	
		if(i > MaxLen - 1)	
		{
			printf(".");
			for(;i > MaxLen;i--)	printf("0");
		}

		for(i = MaxLen - 1;i >=0;i--)
		{							
			if(i == Dec -1)	{
								printf(".");
								printf("%c",R[6][i]+'0');
							}	
			else printf("%c",R[6][i]+'0');
		}
		printf("\n");
	}else
	{
		for(i=MaxLen -1;i >=0;i--)
			printf("%c",R[6][i]+'0');
		printf("\n");
	}

}

int HaveDec(char R[])
{
	int i;
	for(i=0;i <6;i++)
		if(R[i] == '.')		return 1;
	
	return 0;
}

void ProcessInt(char R[][160],int n)
{
	int Len;
	int i = 0,j = strlen(R[0])-1;
	int k;
	char Buff[6];
	memset(Buff,0,sizeof(Buff));

	while(R[0][i] == '0')	i++;
	k = 0;
	for(;i!=j+1;i++)
		Buff[k++] = R[0][i];
	Buff[k] = 0;
	memcpy(R,Buff,sizeof(Buff));

	Reserve(R[0]);
	Len = strlen(R[0]);
	MaxLen = Len;
	Len1 = Len;

	for(i = 0 ;i < Len;i++)/*copy*/
	{
		R[0][i] -= '0';
		R[6][i] = R[0][i];
	}

	for(i = 1;i < n;i++)
	{
		Multiply(R);

		for(j = 1;j <= 5;j++)/*clear memory*/			
			memset(R[j],0,sizeof(R[j]));				
	}


}

int main()
{
	char	R[7][160];
	int		n;
	int		i;
	int		Dec;

	for(i=0;i<7;i++)
		memset(R[i],0,sizeof(R[i]));

	while(scanf("%s%d",R[0],&n) != EOF)
	{	
		IsDec = 1;
		Dec = 0;
		if(n != 1)	
		{	if(HaveDec(R[0]))	
			{
				IsDec = 1;
				Process(R,&Dec,n);
			}
			else ProcessInt(R,n);
		}

		Print(R,Dec,n);
		
		for(i=0;i<7;i++)
			memset(R[i],0,sizeof(R[i]));
	}

	
	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