Big Integer,帮忙测试测试!

软件和网站开发以及相关技术探讨
回复
头像
tlhl28
帖子: 474
注册时间: 2006-09-02 18:58
来自: 深圳

Big Integer,帮忙测试测试!

#1

帖子 tlhl28 » 2008-08-09 15:43

这个题目,我做完上交后还是“wrong answer”。:(
请大家帮忙测试测试结果哪还有错误,哪种情况还算错。先行谢过!:D
Description

Please calculate the answer of A+B and the answer of A-B, both A and B are integer.
Input

The first line of input contains T(1 < T < 1000), the number of test cases. There is only line for each test case. It contains two integers A,B(-10^1000 < A,B < 10^{1000}).
Output

For each test case, output two lines A+B and A-B.
Sample Input

3
1 2
1 1
-1 -1

Sample Output

3
-1
2
0
-2
0

我的代码:

代码: 全选

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

#define MAX 9000

char result[MAX];
int i = 0;

enum {PASS, MINUS};

int sum(char *,char *,int);
int ro(char *);
void free_result();
char *clear_zero(char *e);

int  main()
{
    char  fir[MAX];
    char  sec[MAX];
    char  thr[MAX];
    char *e1,*e2,*chg;
    int l1,l2,len;
    int  cases;
    int get_minus,swap;
    int j;
    int neg_print_minus;
    int pattern;

    int get_negative;
    int onebig;
    int e1_minus, e2_minus, dminus;

    scanf("%d",&cases);
    while(cases--)
    {
	e1 = fir;
	e2 = sec;
	neg_print_minus = 0;
	onebig = 1;
	e1_minus = 0; e2_minus = 0; dminus = 0;
	scanf("%s%s",e1,e2);

	if (*e1 == '-' || *e2 == '-') {
	    if ( *e1 == '-' && *e2 == '-'){
		++e1;
		++e2;
		dminus = 1;
	    } else if (*e1 == '-') {
		++e1;
		e1_minus = 1;
	    } else if (*e2 == '-') {
		++e2;
		e2_minus = 1;
	    }
	    get_negative = 1;
	} else
	    get_negative = 0;

	e1 = clear_zero(e1);
	e2 = clear_zero(e2);
	if (*e1 == '0' && *e2 == '0') {
	    printf("0\n0\n");
	    continue;
	}
	l1 = strlen(e1);
	l2 = strlen(e2);

	len = l1 - l2;
	onebig = 1;
	get_minus = (len <= 0 && strcmp(e1,e2) < 0) ? 1 : 0;
	if (len < 0 || get_minus) {
	    swap = 1;
	    onebig = 0;
	    chg = e1; e1 = e2; e2 = chg;
	    len = -len;
	} else
	    swap = 0;
	if (len != 0) {
	    for (j = 0; thr[j] != '\0'; ++j)
		thr[j] = '\0';
	    while (len--)
		thr[len] = '0';
	    //printf("len: %d,thr:%s\n",len,thr);
	    e2 = strcat(thr,e2);
	    //printf("e2:%s\n",e2);
	} else
	    neg_print_minus = 0;

	for (pattern = PASS,j = 0;j < 2; j++, pattern = MINUS) {
	    if (get_negative && !dminus)
		pattern = (pattern == MINUS) ? PASS : MINUS;
	    //printf("~~ongbig:%d,dminus:%d,pattern:%d~~",onebig,dminus,pattern);
	    if (strcmp(e1,e2) == 0 && pattern == MINUS)
		neg_print_minus = 0;
	    else if (dminus && !(onebig == 0 && pattern == MINUS)) {
		neg_print_minus = 1;
		//printf("~do1~:");
	    } else if (onebig == 0 && e1_minus && pattern == PASS) {
		neg_print_minus = 1;
		//printf("~do2~:");
	    } else if (onebig == 0 && e2_minus && pattern == MINUS) {
		neg_print_minus = 1;
		//printf("~do2.1~:");
	    } else if (onebig && e1_minus) {
		neg_print_minus = 1;
		//printf("~do3~:");
	    } else
		neg_print_minus = 0;

	    if(sum(e1,e2,pattern))
		strcat(result,"1");
	    if (get_negative == 0 && pattern == MINUS && (get_minus || swap))
		printf("-");
	    if (get_negative && neg_print_minus)
		printf("-");
	    if(ro(result) == 1)
		printf("0");	
	    printf("\n");
	    free_result();
	}
    }
    return  0;
}

int  sum(char *e1, char *e2,int pattern)
{
    int re = 0;
    int carry = 0;
    if (*e1 == '\0' && *e2 == '\0')
	return 0;
    else
	carry = sum(++e1,++e2,pattern);
    --e1;
    --e2;

    if (pattern == MINUS ) {
	if ((re = (*e1 - '0') - carry - (*e2 - '0')) < 0) {
	    result[i++] = re + 10 + '0';
	    return 1;
	} else
	    result[i++] = re + '0';
    } else if (pattern == PASS) {
	if ((re = (*e1 - '0') + carry + (*e2 - '0')) >= 10) {
	    result[i++] = re - 10 + '0';
	    return 1;
	} else
	    result[i++] = re + '0';
	result[i] = '\0';
    }
    return 0;
}

int ro(char *in) {
    static int iszero = 0;
    int jzero;
    if (*in == '\0')
	return 1;
    else
	jzero = ro(++in);
    --in;
    if (jzero) {
	if (*in == '0')
	    iszero = 1;
    	else {
	    jzero = 0;
	    iszero = 0;
	}
    }
    if (!iszero)
	printf("%c",*in);
    return jzero;
}

void free_result()
{
    i = 0;
    while (result[i] != '\0')
	result[i++] = '\0';
    i = 0;
}

char *clear_zero(char *e)
{
    if (*e == '0')
	while (*++e == '0' && *e == '\0')
	    ;
    if (*e == '\0')
	*--e = '0';
    return e;
}
上次由 tlhl28 在 2008-08-09 23:03,总共编辑 2 次。
------
ThinkPad·R60i-0657LN1
Ubuntu 8.04 . Hardy Heron+XP
------
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

#2

帖子 BigSnake.NET » 2008-08-09 15:49

哪里的OJ
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
头像
tlhl28
帖子: 474
注册时间: 2006-09-02 18:58
来自: 深圳

#3

帖子 tlhl28 » 2008-08-09 16:12

找出错误就告诉你,哈~ :D
------
ThinkPad·R60i-0657LN1
Ubuntu 8.04 . Hardy Heron+XP
------
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

#4

帖子 BigSnake.NET » 2008-08-09 16:13

输入中带前缀0时貌似有问题
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
头像
BigSnake.NET
帖子: 12522
注册时间: 2006-07-02 11:16
来自: 廣州
联系:

#5

帖子 BigSnake.NET » 2008-08-09 16:28

用了几千个的随机例子,没有找到错误
^_^ ~~~
要理解递归,首先要理解递归。

地球人都知道,理论上,理论跟实际是没有差别的,但实际上,理论跟实际的差别是相当大滴。
头像
tlhl28
帖子: 474
注册时间: 2006-09-02 18:58
来自: 深圳

#6

帖子 tlhl28 » 2008-08-09 16:29

哈~比你早一步,改了还是不行。(悄悄告诉你,是深大的) :D
下面是我的测试弄的。注释的和没注释的都没问题。不知道是哪种特殊情况不行...
下面这个w.c是同学通过的了。

代码: 全选

#!/bin/sh
gcc -Wall cal.c -o cal
gcc -Wall ro.c -o ro
gcc -Wall w.c -o w
./ro > test
cat test | ./w > result2
cat test | ./cal > result
diff -n result result2 > cmp

代码: 全选

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,k,l;
    /*printf("1000000\n");
    for (i = -1000,k = -1000; i < 1000; i++,k++)
	for(j = -1000,l = -1000;j < 1000; j++,l++)
	    printf("%d%d %d%d\n",i,j,k,l);
    */
    printf("801\n");
    for (j = 0;j < 200; ++j)
	    printf("%d%d%d%d%d%d %d%d%d%d%d%d\n",rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand());
    for (j = 0;j < 200; ++j)
	    printf("-%d%d%d%d%d%d -%d%d%d%d%d%d\n",rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand());
    for (j = 0;j < 200; ++j)
	    printf("-%d%d%d%d%d%d %d%d%d%d%d%d\n",rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand());
    for (j = 0;j < 200; ++j)
	    printf("%d%d%d%d%d%d -%d%d%d%d%d%d\n",rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand(),rand());

    return 0;
}
------
ThinkPad·R60i-0657LN1
Ubuntu 8.04 . Hardy Heron+XP
------
头像
tlhl28
帖子: 474
注册时间: 2006-09-02 18:58
来自: 深圳

w.c

#7

帖子 tlhl28 » 2008-08-09 20:13

我要错误的计算结果:em80

代码: 全选

#include<stdio.h>
#include<string.h>
 
#define L 9000
#define M 9000
 
char thi[M];
 
 
void sum(char fir[],char sec[]);          //posi + posi
void nenega(char fir[],char sec[]);       //nega + nega
void minega(char fir[],char sec[]);       //posi + nega
void neposi(char fir[],char sec[]);       //nega + posi
void minus(char fir[],char sec[]);        //posi - posi
void neminus(char fir[], char sec[]);     //nega - nega
void ponega(char fir[], char sec[]);      //posi - nega
void neminusposi(char fir[], char sec[]); //nega - posi
 
int main()
{
	//freopen("text.in","r",stdin);
	//freopen("text.out","w",stdout);
  int cases;
  char fir[L];
  char sec[L];
  scanf("%d",&cases);
  while(cases--)
  {  
	 scanf("%s%s",fir,sec);	  
	 sum(fir,sec);             //posi + posi
	 nenega(fir,sec);          //nega + nega
	 minega(fir,sec);          //posi + nega
	 neposi(fir,sec);          //nega + posi
	 minus(fir,sec);           //posi - posi
	 neminus(fir,sec);         //nega - nega
	 ponega(fir,sec);          //posi - nega
	 neminusposi(fir,sec);     //nega - posi
  }
  return 0;
}
 
void nenega(char fir[],char sec[])   //nega + nega
{
  if(fir[0]!='-'||sec[0]!='-')
  {
	  return;
  }  
  if(strcmp(fir,"-0")==0&&strcmp(sec,"-0")==0)
  {
   printf("0\n");
   return;
  }
  int i,big,small;
  char one[1200],two[1200];
  strcpy(one,fir);
  strcpy(two,sec);
  big=strlen(one);
  small=strlen(two);
  for(i=0;i<big;i++) 
	  one[i]=fir[i+1];      
  for(i=0;i<small;i++)
	  two[i]=sec[i+1]; 
  printf("-");
  sum(one,two);
}
 
void sum(char fir[],char sec[])
{
   if(fir[0]=='-'||sec[0]=='-')
	   return;
   if(strcmp(fir,"0")==0&&strcmp(sec,"0")==0)
    {
	  printf("0\n");
	  return;
    }
   int i,j,big,small,flag=0;
   int carry=0,t=0;
   char result[1200];
   for(i=0;i<1005;i++)
	   result[i]='0';
   char one[M],two[M],three[M];
   strcpy(one,fir);
   strcpy(two,sec);
 
 
  big=strlen(fir);
  for(i=0;i<big;i++)
  {
    while(fir[0]=='0')
		for(j=0;j<big;j++)
			 fir[j]=fir[j+1];
  }
 
   small=strlen(sec);
  for(i=0;i<small;i++)
  {
    while(sec[0]=='0')
		for(j=0;j<small;j++)
			 sec[j]=sec[j+1];
  }
 
 
   big=strlen(fir);
   small=strlen(sec);
 
   if(big<small)
   {
	   strcpy(three,fir);
	   strcpy(fir,sec);
	   strcpy(sec,three);
	   big=strlen(fir);
       small=strlen(sec);
   }
 
     for(j=big-1,i=small-1;j>=0;j--,i--)
	   {
		   if(i>=0)
		   {			
				thi[j]=(  (fir[j]-'0'+sec[i]+carry) %58)+'0';
 
				if(fir[j]-'0'+sec[i]+carry>'9')
				{
					thi[j]=((fir[j]-'0'+sec[i]+carry) %58)+'0';
					carry=1;
				}
				else 
				{		
					thi[j]=fir[j]-'0'+sec[i]+carry;
					carry=0;
				}
		   }
 
		   else
		   {	 
			   if(fir[j]+carry>=58)
			   {		
			     thi[j]=(fir[j]+carry)%58+'0';
				 carry=1;
			   }
			   else
			   {		
			     thi[j]=fir[j]+carry;
				 carry=0;
			   }
		   }
	   }
	 thi[big]='\0';
 
	 strcpy(result,thi);
     if(carry==1)
	 {
		 result[0]='1';
	     for(i=0;i<=big;i++)
		    result[i+1]=thi[i]; 
	 }
	 printf("%s",result);
 
 
   strcpy(fir,one);
   strcpy(sec,two);
   printf("\n");
}
 
void minega(char fir[],char sec[])   //posi + nega
{
	if(fir[0]=='-'||sec[0]!='-')
	{
		return;
	}
  int i,j,small;
  char two[M];
  strcpy(two,sec);
  small=strlen(sec);
  for(i=0;i<small;i++)
	  two[i]=two[i+1];
 
  for(i=0;i<small;i++)
  {
    while(two[0]=='0')
		for(j=0;j<small;j++)
			 two[j]=two[j+1];
  }
 
 
 
  minus(fir,two);      //posi - posi
}
 
void neposi(char fir[],char sec[])  //nega + posi
{
  if(fir[0]!='-'||sec[0]=='-')
	  return;
  if(strcmp(fir,"-0")==0&&strcmp(sec,"0")==0)
    {
	  printf("0\n");
	  return;
    }
  int i,j,big;
  char three[M];
//printf("nega + posi");
  big=strlen(fir);
  for(i=0;i<big;i++)
  {
    three[i]=fir[i+1];  
  }
  for(i=0;i<big;i++)
  {
    while(three[0]=='0')
		for(j=0;j<big;j++)
			 three[j]=three[j+1];
  }
 
  minus(sec,three) ;     //posi - posi
}
 
void minus(char fir[],char sec[])   // posi -  posi
{
  if(fir[0]=='-'||sec[0]=='-')
    {  
	  return;
    }
  if(strcmp(fir,sec)==0)
    {
	  printf("0\n");
	  return;
    }
  //printf("posi -  posi\n");
  char one[M],two[M];
  strcpy(one,fir);
  strcpy(two,sec);
  int i,j,big,small;
 
  big=strlen(fir);
  for(i=0;i<big;i++)
  {
    while(fir[0]=='0')
		for(j=0;j<big;j++)
			 fir[j]=fir[j+1];
  }
 
  small=strlen(sec);
  for(i=0;i<small;i++)
  {
    while(sec[0]=='0')
		for(j=0;j<small;j++)
			 sec[j]=sec[j+1];
  }
 
  big=strlen(fir);
  small=strlen(sec);
 
  if(big>=small)    
   {
	  if(strcmp(fir,sec)>=0||big>small)   
	  {
		for(i=small-1,j=big-1;j>=0;i--,j--)
		{
			   if(i>=0)
				{
					if(fir[j]>=sec[i])
					{					
					thi[j]=fir[j]-sec[i]+'0';
					}			    
					else
					{				 
					     thi[j]=fir[j]+10-sec[i]+'0';
					     fir[j-1]--;
					}
				}
 
		      else
				{				
					if((fir[j])<'0')
					{						 	
						thi[j]=fir[j]+10;
						fir[j-1]--;
					}
					else
					{		
					    thi[j]=fir[j];						
					}				
				}
		}
	    i=0;
		while(thi[i]=='0')
			i++;
		for(i;i<big;i++)
		  printf("%c",thi[i]);
 
		printf("\n");  
     }
 
	else  
		{
			printf("-");			
			minus(sec,fir);      //posi - posi
		}  
    }
 
 
  else if(big<small)
	 {
	      printf("-");		  
		  minus(sec,fir);        //posi - posi
	 }
 
  strcpy(fir,one);
  strcpy(sec,two);
 
} 
 
 
void neminus(char fir[], char sec[])       //nega - nega
{
  if(fir[0]!='-'||sec[0]!='-')
	  return;
  if(strcmp(fir,sec)==0)
  {
	  printf("0\n");
	  return;
  }
  int i,small;
  char one[M],two[M];
  char three[M];
  strcpy(one,fir);
  strcpy(two,sec);
  small=strlen(two);
  for(i=0;i<=small;i++)
  {
    three[i]=two[i+1];  
  }
  neposi(one,three);               //nega + posi
 
}
 
void  ponega(char fir[], char sec[])        // posi - nega
{
   if(fir[0]=='-'||sec[0]!='-')
   {
	   return;
   }
   //printf("posi- nega");
   int i,j,small;
   char one[M],two[M];
   strcpy(one,fir);
   strcpy(two,sec);
   small=strlen(two);
 
   for(i=0;i<small;i++)
	  two[i]=two[i+1];
 
  for(i=0;i<small;i++)
  {
    while(two[0]=='0')
		for(j=0;j<=small;j++)
			 two[j]=two[j+1];
  }
 
   sum(one,two);   //posi + posi                       
}
 
void neminusposi(char fir[], char sec[])    //  nega - posi
{
  if(fir[0]!='-'||sec[0]=='-')
   {
	   return;
   }
  int i,small;
  char one[M],two[M];
  char three[M];
  strcpy(one,fir);
  strcpy(two,sec);
  three[0]='-';
  small=strlen(two);
  for(i=0;i<=small;i++)
  {
    three[i+1]=two[i];  
  }
  nenega(one,three);    //nega + nega
}
------
ThinkPad·R60i-0657LN1
Ubuntu 8.04 . Hardy Heron+XP
------
回复