#0: 100% WA (line:7)
您的答案為: MMCCXVII
正確答案為: MCMXCVII
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
# include <math.h>
int gNum1 = 0 ;
int gNum2 = 0 ;
void turnToNumber( char s[] ) {
int boo = 0 ;
int length = strlen( s ) ;
int sum = 0 ;
while ( boo < 2 ) {
for ( int i = 0 ; i < length ; i++ ) {
if ( s[i] == 'I' ) sum = sum + 1 ;
else if ( s[i] == 'V' ) sum = sum + 5 ;
else if ( s[i] == 'X' ) sum = sum + 10 ;
else if ( s[i] == 'L' ) sum = sum + 50 ;
else if ( s[i] == 'C' ) sum = sum + 100 ;
else if ( s[i] == 'D' ) sum = sum + 500 ;
else if ( s[i] == 'M' ) sum = sum + 1000 ;
} // for
if ( boo == 0 ) {
gNum1 = sum ;
} // if
else if ( boo == 1 ) {
gNum2 = sum ;
break ;
} // else if
boo = boo + 1 ;
scanf( "%s", s ) ;
length = strlen( s ) ;
sum = 0 ;
} // while
} // turnToNumber()
void turnToRN( int sum ) {
char s[10] = {'\0'}, rn[15] = {'\0'} ;
sprintf( s, "%d", sum ) ;
int length = strlen( s ) ;
int n = 0 ;
for ( int i = 0 ; length > 0 ; i++ ) {
n = ( int ) s[i] - 48 ;
if ( length == 4 ) {
while ( n > 0 ) {
n = n - 1 ;
strcat( rn, "M" ) ;
} // while
} // if
else if ( length == 3 ) {
while ( n > 0 ) {
if ( n == 9 ) {
n = 0 ;
strcat( rn, "CM" ) ;
} // if
else if ( n == 4 ) {
n = 0 ;
strcat( rn, "CD" ) ;
} // else if
else if ( n >= 5 ) {
n = n - 5 ;
strcat( rn, "D" ) ;
} // else if
else {
n = n - 1 ;
strcat( rn, "C" ) ;
} // else
} // while
} // else if
else if ( length == 2 ) {
while ( n > 0 ) {
if ( n == 9 ) {
n = 0 ;
strcat( rn, "XC" ) ;
} // if
else if ( n == 4 ) {
n = 0 ;
strcat( rn, "XL" ) ;
} // else if
else if ( n >= 5 ) {
n = n - 5 ;
strcat( rn, "L" ) ;
} // else if
else {
n = n - 1 ;
strcat( rn, "X" ) ;
} // else
} // while
} // else if
else if ( length == 1 ) {
while ( n > 0 ) {
if ( n == 9 ) {
n = 0 ;
strcat( rn, "IX" ) ;
} // if
else if ( n == 4 ) {
n = 0 ;
strcat( rn, "IV" ) ;
} // else if
else if ( n >= 5 ) {
n = n - 5 ;
strcat( rn, "V" ) ;
} // else if
else {
n = n - 1 ;
strcat( rn, "I" ) ;
} // else
} // while
} // else if
n = 0 ;
length = length - 1 ;
} // for
printf( "%s\n", rn ) ;
} // turnToRN()
int main( ) {
char s[15] = {'\0'} ;
scanf( "%s", s ) ;
int sum = 0 ;
while ( strcmp( s, "#" ) != 0 ) {
turnToNumber( s ) ;
sum = gNum1 - gNum2 ;
if ( sum == 0 ) {
printf( "ZERO\n" ) ;
} // if
else {
if ( sum < 0 ) turnToRN( 0 - sum ) ;
else turnToRN( sum ) ;
} // else
gNum1 = 0 ;
gNum2 = 0 ;
sum = 0;
scanf( "%s", s ) ;
} // while
return 0;
} // main()
for ( int i = 0 ; i < length ; i++ ) {
if ( s[i] == 'I' ) sum = sum + 1 ;
else if ( s[i] == 'V' ) sum = sum + 5 ;
else if ( s[i] == 'X' ) sum = sum + 10 ;
else if ( s[i] == 'L' ) sum = sum + 50 ;
else if ( s[i] == 'C' ) sum = sum + 100 ;
else if ( s[i] == 'D' ) sum = sum + 500 ;
else if ( s[i] == 'M' ) sum = sum + 1000 ;
} // for
要用減法規則,例如IV是4