Follows 389.c (Total 64 lines):

/* @JUDGE_ID:4461XX 389 C */
/* A */
#include<stdio.h>
#include<string.h>
#include<math.h>

#define MAX_LEN 10000

int turn10( char *arr , int base , int *error )
{
	int i , ans ;

	for( i=0,ans=0 ; i<strlen( arr ) ; ++i ){
		ans *= base ;

		if( ( '0'<=arr[i]&&arr[i]<='9' )&&( arr[i]-'0'>=base ) ||
			( 'A'<=arr[i]&&arr[i]<='F' )&&( arr[i]-'A'+10>=base ) ){ /* error */
			*error = 1 ;
			break ;
		}

		if( '0'<=arr[i]&&arr[i]<='9' ) ans += arr[i]-'0' ;
		else ans += arr[i]-'A'+10 ;
	}

	return ans ;	
}
void turninto( int num , int base )
{
	int i , tmp ;
	
	if( num==0 ){
		puts( "      0" );
		return;
	}

	for( i=6 ; i>=0&&(int)pow( base , i )>num ; --i ) putchar( ' ' ) ;

	for( ; i>=0 ; --i ){
		tmp = num/(int)pow( base , i ) ;
		num %= (int)pow( base , i ) ;

		if( tmp>=10 ) putchar( tmp-10+'A' ) ;
		else putchar( tmp+'0' ) ;
	}

	putchar( '\n' ) ;
}
int main( void )
{
	char arr[MAX_LEN] ;
	int base , into , num10 , error ;
	
	while( scanf( "%s %d %d\n" , arr , &base , &into )==3 ){
		error = 0 ;
		num10 = turn10( arr , base , &error ) ;
		if( num10>=(int)pow( into , 7 ) ) error = 1 ;

		if( !error ) turninto( num10 , into ) ;
		else puts( "  ERROR" ) ;
	}

	return 0 ;
}

Back to statistics
Ya-Lin Huang (C)
huangyl@gmail.com