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 lantianjidi at 2008-11-24 16:33:39 on Problem 1396 and last updated at 2008-11-24 16:42:47
我的代码在zoj上可以过(题目是2017),但是在这里就过不了,很诡异呀。
代码如下:

#include <iostream>
//#include <string>
#include <stdio.h>
using namespace std;

const int MAX=1020;
const int MAP=510;
int tmp[MAP];
int bi[MAX];

void substract(int a[],int b[])
{
	int i;
	
	for(i=0; i<MAP; i++)
	  a[i]=a[i]-b[i];
	for(i=0; i<MAP-1; i++)
	  if(a[i]<0)
	  {
	  	a[i+1]--;
	  	a[i]=10+a[i];
	  }
}

void add(int a[],int b[])
{
	int i;
	
	for(i=0; i<MAP; i++)
	  b[i]=a[i]+b[i];
	for(i=0; i<MAP-1; i++)
	{
		b[i+1]+=b[i]/10;
		b[i]=b[i]%10;
	}	
}

void mulp(int a[],int b[],int lena,int lenb)
{
	int i,j;
	
	for(i=0;i<lenb;i++)
	{
		for(j=0;j<lena;j++)
		{
		  bi[i+j]+=a[j]*b[i];
		}
	}
	
	for(i=0;i<MAX-1;i++)
	{
		bi[i+1]+=bi[i]/10;
		bi[i]%=10;
	}
}

int main()
{
	int i,j;
	int n;
	int a[MAP],b[MAP];
	char st[MAX];
	
	scanf("%d",&n);
	for(i=0; i<n; i++)
	{
		scanf("%s",st);
		int len=strlen(st);
		for(j=0; j<len; j++)
		  if(st[j]=='+' || st[j]=='-' || st[j]=='*')
		  	break;
		int operpos=j;
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		int lena=operpos;
		int lenb=len-(operpos+1);
		for(j=0; j<lena; j++)
		  a[lena-1-j]=st[j]-'0';
		for(j=operpos+1; j<len; j++)
		  b[(lenb-1)-(j-(operpos+1))]=st[j]-'0';
		
		if(st[operpos]=='-')
		{
			substract(a,b);
			for(j=MAP-1;j>=0;j--)
			  if(a[j]!=0)
			  	break;
			int nlena=j+1;
			
			int newlena;
			if(j<0)
			  newlena=1;
			else
				newlena=nlena;
				
		  int qb=(nlena>(lenb+1))?nlena:(lenb+1);
		  nlena=(lena>(lenb+1))?lena:(lenb+1);
		  
		  for(j=0;j<nlena-lena;j++)
		    printf(" ");
			for(j=0; j<operpos; j++)
			  printf("%c",st[j]);
			printf("\n");
			 
			for(j=0;j<nlena-(lenb+1); j++)
			  printf(" ");
			printf("-");
			for(j=operpos+1; j<len; j++)
			  printf("%c",st[j]);
			printf("\n");
			
		  for(j=0;j<nlena-qb;j++)
		    printf(" ");	
			for(j=0;j<qb;j++)
			  printf("-");
			printf("\n");
			
			for(j=0;j<nlena-newlena;j++)
			  printf(" ");
			for(j=newlena-1;j>=0;j--)
			  printf("%d",a[j]);
			cout<<endl;
		}
		else
			if(st[operpos]=='+')
			{
				add(a,b);
				
				for(j=MAP-1; j>=0; j--)
				  if(b[j]!=0)
				  	break;
				int lenbn=j+1;
				int newlenb;
				if(j<0)
					newlenb=1;
				else
					newlenb=lenbn;
					
				int dade=(lena>(lenb+1))?lena:(lenb+1);
        dade=(dade>lenbn)?dade:lenbn;
        
        for(j=0; j<dade-lena; j++)
          printf(" ");
			  for(j=0; j<operpos; j++)
			    printf("%c",st[j]);
			  printf("\n");
			  //cout<<endl;
			  
			  for(j=0; j<dade-(lenb+1); j++)
			    printf(" ");
			  printf("+");
			  for(j=operpos+1; j<len; j++)
			    printf("%c",st[j]);
			  printf("\n");
			  
			  for(j=0; j<dade; j++)
			    printf("-");
			  printf("\n");
			  
			  for(j=0; j<dade-newlenb; j++)
			    printf(" ");
			  for(j=newlenb-1; j>=0; j--)
			    printf("%d",b[j]);
			  printf("\n");		
			}
			else
			{	
				memset(bi,0,sizeof(bi));
				mulp(a,b,lena,lenb);
				
        for(j=MAX-1; j>=0; j--)
          if(bi[j]!=0)
          	break;
        int lenlast;
        int lenf;
        
        if(j<0)
        	lenlast=1;
        else
        	lenlast=j+1;
        lenf=lenlast;
        int big=(lena>(lenb+1))?lena:(lenb+1);
        lenlast=(lenlast>big)?lenlast:big;
        
        for(j=0; j<lenlast-lena; j++)
          printf(" ");
        for(j=0; j<operpos; j++)
          printf("%c",st[j]);
        printf("\n");	
        
        for(j=0; j<lenlast-(lenb+1); j++)
          printf(" ");
        printf("*");
        for(j=operpos+1; j<len; j++)
          printf("%c",st[j]);
        printf("\n");
        
        
        memset(tmp,0,sizeof(tmp));
        for(j=0;j<lena;j++)
          tmp[j]=a[j]*b[0];
        for(j=0;j<MAP-1;j++)
        {
        	tmp[j+1]+=tmp[j]/10;
        	tmp[j]%=10;
        }
        for(j=MAP-1; j>=0; j--)
          if(tmp[j]!=0)
          	break;
        
        int qm;
        if(j>=0)
        {
          qm=j+1;
          big=((lenb+1)>qm)?(lenb+1):qm;
        }
        else
        {
        	qm=1;
        	big=((lenb+1)>qm)?(lenb+1):qm;
        	}
        
        if(lenb>1)
        {
          for(j=0; j<lenlast-big; j++)
            printf(" ");
          for(j=0; j<big; j++)
            printf("-");
          printf("\n");
        }
        else
        {
        	int mb=(lenf>(lenb+1))?lenf:(lenb+1);
        	for(j=0;j<lenlast-mb;j++)
        	  cout<<" ";
        	for(j=0; j<mb; j++)
        	  printf("-");
        	printf("\n");
        }
        
        int wx;
        int bf,k;
        if(lenb>=2)
        {
          for(j=0; j<lenb; j++)
          {
          	memset(tmp,0,sizeof(tmp));
          	for(k=0;k<lena;k++)
          	  tmp[k]=a[k]*b[j];
          	
          	for(k=0;k<MAP-1;k++)
          	{
          		tmp[k+1]+=tmp[k]/10;
          		tmp[k]%=10;
          	}
          	
        	  for(k=MAP-1; k>=0; k--)
        	    if(tmp[k]!=0)
        	  	  break;
        	
          	if(k<0)
        		  wx=lenlast-j-1;
        	  else
        		  wx=lenlast-j-(k+1);
        	
        	  if(j==lenb-1)
        		  bf=lenlast-wx;
        	
        	  int p;
        	  for(p=0; p<wx; p++)
        	    cout<<" ";
        	  if(k<0)
        		  cout<<"0";
        	  else
        	  {
        		  for(p=k; p>=0; p--)
        		    cout<<tmp[p];
        	  }
        	  cout<<endl;
          }
        
          bf=(bf>lenf)?bf:lenf;
          for(j=0; j<lenlast-bf; j++)
            cout<<" ";
          for(j=0; j<bf; j++)
            cout<<"-";
          cout<<endl;
        }
      
        for(j=0; j<lenlast-lenf; j++)
          printf(" ");
        for(j=lenf-1; j>=0; j--)
          printf("%d",bi[j]);
        printf("\n");
			}
			
		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