求助 WA #9 too long on line 1
查看原帖
求助 WA #9 too long on line 1
232887
BlueSu楼主2020/11/30 18:49

RT,不知道哪里错了,请大佬多多指教qwq

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <climits>
#include <string>
using namespace std;

string c1 , c2 ;
int a[20000] , b[20000] , diff[80000] ;

int main () {

    cin >> c1 >> c2 ;
    if ( c1 < c2 && c1.size () <= c2.size () ) {
        printf ( "-" ) ;
        swap ( c1 , c2 ) ;
    }
    else if ( c1.size () < c2.size () ) {
        printf ( "-" ) ;
        swap ( c1 , c2 ) ;
    }

    int len1 = c1.size () , len2 = c2.size () ;

    for ( int i = 0 ; i < len1 ; i++ ) {
        a[i + 1] = c1[len1 - i - 1] - '0' ;
    }
    for ( int j = 0 ; j < len2 ; j++ ) {
        b[j + 1] = c2[len2 - j - 1] - '0' ;
    }

    int len = max ( len1 , len2 ) ;
    for ( int i = 1 ; i <= len + 5 ; i++ ) {
        diff[i] = a[i] - b[i] ;
    }

    for ( int i = 1 ; i <= len + 5 ; i++ ) {
        if ( diff[i] < 0 ) {
            diff[i + 1] -- ;
            diff[i] += 10 ;
        }
    }

    // cout << len1 << " " << len2 ;

    int flag = true ;
    for ( int i = len + 5 ; i >= 1 ; i-- ) {
        if ( flag && diff[i] == 0 ) {
            continue ;
        }
        // if ( flag ) {
        //     cout << " " << i ;
        // }
        flag = false ;
        printf ( "%d" , diff[i] ) ;
    }

    if ( diff[1] == 0 ) {
        printf ( "0" ) ;
    }

    return 0 ;
}
2020/11/30 18:49
加载中...