1023 Have Fun with Numbers
解题代码
#include <cstdio>
#include <cstring>
char str[ 20 ] ;
int num[ 10 ] ;
struct bign {
int len;
int p[ 21 ] ;
bign ( ) {
len = 0 ;
memset ( p, 0 , sizeof ( p) ) ;
}
} ;
bign change ( char str[ ] ) {
bign a;
a. len = strlen ( str) ;
for ( int i = 0 ; i < a. len; i++ )
a. p[ i] = str[ a. len - i - 1 ] - '0' ;
return a;
}
void print ( bign a) {
for ( int i = 0 ; i < a. len; i++ )
printf ( "%d" , a. p[ a. len - i - 1 ] ) ;
}
bool judge ( void ) {
for ( int i = 0 ; i < 10 ; i++ )
if ( num[ i] ) return false ;
return true ;
}
bign multi ( bign a, int b) {
bign c;
int carry = 0 ;
for ( int i = 0 ; i < a. len; i++ ) {
int temp = a. p[ i] * b + carry;
carry = temp / 10 ;
c. p[ c. len++ ] = temp % 10 ;
}
while ( carry) {
c. p[ c. len++ ] = carry % 10 ;
carry / = 10 ;
}
return c;
}
int main ( ) {
scanf ( "%s" , str) ;
bign a = change ( str) ;
for ( int i = 0 ; i < a. len; i++ )
num[ a. p[ i] ] ++ ;
bign b = multi ( a, 2 ) ;
for ( int i = 0 ; i < b. len; i++ )
num[ b. p[ i] ] -- ;
if ( judge ( ) ) printf ( "Yes\n" ) ;
else printf ( "No\n" ) ;
print ( b) ;
return 0 ;
}
测试结果
问题整理
1.大整数运算。
2.注意乘法后位数会增加,故要预留。