题目指路:P1932 输出大数的加减乘除模结果 语言:C语言 经fc比较,测试点1每一行最后都比我多一个空格,但我哪怕每行输出添加空格也是wa。 注:原本是tle,在我添加了判断除数为0的情况后,全部测试点由tle变成了wa。然而在测试点1中,并不存在除数为0. PS:我不需要已经AC的代码,我只想知道为什么我和测试点的输出是一致的却被判为wa。 代码如下:
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<stdarg.h>
#include<string.h>
int InputEnd=0;
struct BigInteger{
char A[100010];
int num;
};
struct BigInteger Check(struct BigInteger* this){// fix this
while ( this->num>0 && this->A[this->num]==0 )
this->num--;
for ( int i=1 ; i<=this->num ; i++ ){
this->A[i+1]+=this->A[i]/10;
this->A[i]%=10;
if ( this->A[i]<0 && (i!=this->num || this->A[i+1]) ){
this->A[i+1]--;
this->A[i]+=10;
}
if ( this->A[this->num+1]!=0 )
this->num++;
}
while ( this->num>0 && this->A[this->num]==0 )
this->num--;
if ( this->A[this->num]<0 ){
this->A[0]='-'+'+'-this->A[0];
for ( int i=1 ; i<=this->num ; i++ )
this->A[i]=-this->A[i];
Check(this);
}
return *this;
}
struct BigInteger GiveValueto(struct BigInteger* this,int a){// this = (BigInteger)a
memset(this->A,0,sizeof(this->A));
if ( a<0 ){
this->A[0]='-';
a=-a;
}
else
this->A[0]='+';
this->num=0;
while ( a>0 ){
this->A[++this->num]=a%10;
a/=10;
}
return *this;
}
struct BigInteger Value(int a){// (BigInteger) a
struct BigInteger ret;
GiveValueto(&ret,a);
return ret;
}
struct BigInteger ValueCopy(struct BigInteger* this,struct BigInteger a){// this=a
this->num=a.num;
memcpy(this->A,a.A,sizeof(this->A));
return *this;
}
int Input(struct BigInteger* this){// scanf(this) return 1 if success while 0 if fail
if ( InputEnd )
return 0;
char r=0;
GiveValueto(this,0);
do{
r=getchar();
if ( !~r ){
InputEnd=1;
return 0;
}
}while ( r==' ' || r=='\n' );
if ( !isdigit(r) ){
if ( r=='-' )
this->A[0]=r;
else
return 0;
}
else
this->A[++this->num]=r-'0';
while ( 1 ){
r=getchar();
if ( !~r ){
InputEnd=1;
break;
}
else if ( !isdigit(r) )
break;
else
this->A[++this->num]=r-'0';
}
for ( int i=1 ; i<=this->num>>1 ; i++ ){
r=this->A[i];
this->A[i]=this->A[this->num+1-i];
this->A[this->num+1-i]=r;
}
return 1;
}
int Output(struct BigInteger a){// printf(this)
Check(&a);
if ( a.A[0]=='-' )
putchar('-');
if ( a.num==0 )
putchar('0');
for ( int i=a.num ; i>0 ; i-- )
putchar(a.A[i]+'0');
return 1;
}
int BIcompare(struct BigInteger a,struct BigInteger b){// a>b 1; a=b 0; a<b -1;
Check(&a);Check(&b);
if ( a.A[0]=='+' && b.A[0]=='-' )
return 1;
else if ( a.A[0]=='-' && b.A[0]=='+' )
return -1;
else if ( a.A[0]=='+' && b.A[0]=='+' ){
if ( a.num>b.num )
return 1;
else if ( a.num<b.num )
return -1;
else{
for ( int i=a.num ; i>0 ; i-- )
if ( a.A[i]>b.A[i] )
return 1;
else if ( a.A[i]<b.A[i] )
return -1;
return 0;
}
}
else{
if ( a.num>b.num )
return -1;
else if ( a.num<b.num )
return 1;
else{
for ( int i=a.num ; i>0 ; i-- )
if ( a.A[i]>b.A[i] )
return -1;
else if ( a.A[i]<b.A[i] )
return 1;
return 0;
}
}
}
struct BigInteger BImax(int count,...){// (count,P1,P2,...,Pcount) return max(Pi)
va_list ap;
va_start(ap,count);
struct BigInteger ret,p;
ValueCopy(&ret,va_arg(ap,struct BigInteger));
count--;
while ( count-- ){
p=va_arg(ap,struct BigInteger);
if ( BIcompare(p,ret)==1 )
ValueCopy(&ret,p);
}
va_end(ap);
return ret;
}
struct BigInteger BImin(int count,...){// return min(Pi)
va_list ap;
va_start(ap,count);
struct BigInteger ret,p;
ValueCopy(&ret,va_arg(ap,struct BigInteger));
count--;
while ( count-- ){
p=va_arg(ap,struct BigInteger);
if ( BIcompare(p,ret)==-1 )
ValueCopy(&ret,p);
}
va_end(ap);
return ret;
}
struct BigInteger Add(struct BigInteger* this,struct BigInteger a){// this+=a; return this;
if ( a.A[0]!=this->A[0] )
for ( int i=1 ; i<=a.num ; i++ )
this->A[i]-=a.A[i];
else
for ( int i=1 ; i<=a.num ; i++ )
this->A[i]+=a.A[i];
if ( a.num>this->num )
this->num=a.num;
Check(this);
return *this;
}
struct BigInteger Subtract(struct BigInteger* this,struct BigInteger a){// this-=a; return this;
a.A[0]='-'+'+'-a.A[0];
return Add(this,a);
}
struct BigInteger Multiply(struct BigInteger* this,struct BigInteger a){// this*=a; return this;
struct BigInteger res;
GiveValueto(&res,0);
for ( int i=1 ; i<=this->num ; i++ )
for ( int j=1 ; j<=a.num ; j++ ){
res.A[i+j-1]+=this->A[i]*a.A[j];
res.A[i+j]+=res.A[i+j-1]/10;
res.A[i+j-1]%=10;
}
res.num=this->num+a.num-1;
res.A[0]=(this->A[0]==a.A[0])?'+':'-';
Check(&res);
ValueCopy(this,res);
return res;
}
struct BigInteger Divide(struct BigInteger* this,struct BigInteger a){// return this%a;this/=a;
if ( !BIcompare(a,Value(0)) )
return Value(0);
struct BigInteger res;
GiveValueto(&res,0);
char thiszf=this->A[0],azf=a.A[0];
this->A[0]='+';
a.A[0]='+';
if ( !~BIcompare(*this,a) ){
this->A[0]=thiszf;
struct BigInteger temp;
ValueCopy(&temp,*this);
ValueCopy(this,res);
return temp;
}
int deltanum=this->num-a.num;
for ( int i=a.num ; i>0 ; i-- )
a.A[i+deltanum]=a.A[i];
for ( int i=1 ; i<=deltanum ; i++ )
a.A[i]=0;
a.num+=deltanum;
if ( !~BIcompare(*this,a) ){
if ( deltanum>0 )
for ( int i=deltanum ; i<a.num ; i++ )
a.A[i]=a.A[i+1];
deltanum--;
a.A[a.num]=0;
a.num--;
}
res.num=deltanum+1;
while ( deltanum>=0 ){
res.A[deltanum+1]++;
Subtract(this,a);
while ( !~BIcompare(*this,a) && deltanum>=0 ){
if ( deltanum>0 )
for ( int i=deltanum ; i<a.num ; i++ )
a.A[i]=a.A[i+1];
deltanum--;
a.A[a.num]=0;
a.num--;
}
}
Check(this);
Check(&res);
struct BigInteger mod;
ValueCopy(&mod,*this);
ValueCopy(this,res);
if ( thiszf!=azf && this->num>0 )
this->A[0]='+'+'-'-this->A[0];
if ( thiszf=='-' && mod.num>0 )
mod.A[0]='-';
return mod;
}
struct BigInteger a,b,c;
int main(){
Input(&a);
Input(&b);
ValueCopy(&c,a);
Output(Add(&a,b));
putchar('\n');
ValueCopy(&a,c);
Output(Subtract(&a,b));
putchar('\n');
ValueCopy(&a,c);
Output(Multiply(&a,b));
putchar('\n');
ValueCopy(&a,c);
ValueCopy(&c,Divide(&a,b));
Output(a);
putchar('\n');
Output(c);
putchar('\n');
return 0;
}