当前位置:网站首页>Template_ Large integer subtraction_ Regardless of size

Template_ Large integer subtraction_ Regardless of size

2022-07-04 20:06:00 This question AC sleep again

//
#include<bits/stdc++.h>
using namespace std;
#define mem( a,v ) memset( a,v,sizeof( a ) )

const int N=11111;
bool f=false;

void re( char *a,char *b )
{   							//  Why can't you flip directly in the function  
    if( strlen(a)>strlen(b) ) return ;
    if( strlen(a)<strlen(b) || strcmp( a,b )<0 ) f=true;
}

int main()
{
    char a[N],b[N],ans[N];
    int len,i;
    mem( a,0 ); mem( b,0 ); mem( ans,0 );

    cin>>a>>b;
    re( a,b ); 
	if( f ) swap( a,b );				// swap 
	
    len=strlen(a);						// len
    reverse( a,a+strlen(a) );
    reverse( b,b+strlen(b) );
    for( i=0;a[i];i++ ) a[i]-='0';
    for( i=0;b[i];i++ ) b[i]-='0';

    for( i=0;i<=len;i++ )
    {
        if( a[i]<b[i] ) a[i+1]--,a[i]+=10;
        ans[i]=a[i]-b[i];
    }
    
    if( f ) cout<<"-";					// -
    for( i=len;i&&ans[i]==0;i-- );
    for( ;~i;i-- ) cout<<(int)ans[i];   // int
    cout<<endl;

    return 0;
}

原网站

版权声明
本文为[This question AC sleep again]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/185/202207041714264092.html