#2625: test case 6 fail.. (pascal)跪求高手打救


Kwing1125 (Ka-wing)

學校 : 元朗商會中學
編號 : 7614
來源 : [203.218.160.191]
最後登入時間 :
2010-05-30 15:17:30
a013. 羅馬數字 -- NPSC 模擬試題 | From: [203.218.200.45] | 發表日期 : 2009-11-08 20:12

與正確輸出不相符(line:6)
您的答案為: VI
正確答案為: IV

program ZeroJudge_a013 ;
var s1, s2, ans : array[ 1..30 ] of char ;
    ch : char ;
    flag : boolean ;
    i, cnt1, cnt2, num1, num2, dif  : longint ;
begin
     while ch <> '#' do
     begin
          flag := false ;
          cnt1 := 0 ;    cnt2 := 0 ;
          while not eoln do
          begin
               read( ch ) ;
               if ch = ' ' then flag := true else
               if flag = true then
               begin
                    inc( cnt2 ) ;
                    s2[ cnt2 ] := ch
               end else begin
                             inc( cnt1 ) ;
                              s1[ cnt1 ] := ch ;
                        end ;
          end ;
          readln ;

          if ch <> '#' then
          begin
               num1 := 0 ;    num2 := 0 ;
               for i := 1 to cnt1 do
               case s1[ i ] of
               'I' : if ( s1[ i+1 ] = 'V' ) or ( s1[ i+1 ] = 'X' ) then
                   num1 := num1 - 1 else num1 := num1 + 1 ;
               'V' : if ( s1[ i+1 ] = 'L' ) or ( s1[ i+1 ] = 'C' ) then
                   num1 := num1 - 5 else num1 := num1 + 5 ;
               'X' : if ( s1[ i+1 ] = 'L' ) or ( s1[ i+1 ] = 'C' ) then
                   num1 := num1 - 10 else num1 := num1 + 10 ;
               'L' : if ( s1[ i+1 ] = 'D' ) or ( s1[ i+1 ] = 'M' ) then
                   num1 := num1 - 50 else num1 := num1 + 50 ;
               'C' : if ( s1[ i+1 ] = 'D' ) or ( s1[ i+1 ] = 'M' ) then
                   num1 := num1 - 100 else num1 := num1 + 100 ;
               'D' : num1 := num1 + 500 ;
               'M' : num1 := num1 + 1000 ;
               end ;

               for i := 1 to cnt2 do
               case s2[ i ] of
               'I' : if ( s2[ i+1 ] = 'V' ) or ( s2[ i+1 ] = 'X' ) then
                   num2 := num2 - 1 else num2 := num2 + 1 ;
               'V' : if ( s2[ i+1 ] = 'L' ) or ( s2[ i+1 ] = 'C' ) then
                   num2 := num2 - 5 else num2 := num2 + 5 ;
               'X' : if ( s2[ i+1 ] = 'L' ) or ( s2[ i+1 ] = 'C' ) then
                   num2 := num2 - 10 else num2 := num2 + 10 ;
               'L' : if ( s2[ i+1 ] = 'D' ) or ( s2[ i+1 ] = 'M' ) then
                   num2 := num2 - 50 else num2 := num2 + 50 ;
               'C' : if ( s2[ i+1 ] = 'D' ) or ( s2[ i+1 ] = 'M' ) then
                   num2 := num2 - 100 else num2 := num2 + 100 ;
               'D' : num2 := num2 + 500 ;
               'M' : num2 := num2 + 1000 ;
               end ;

               dif := abs( num1-num2 ) ;

               for i := 1 to 30 do
               ans[ i ] := chr(0) ;

               if dif = 0 then writeln( 'ZERO' )
               else begin
                         i := 1 ;
                         while dif <> 0 do
                         if dif div 1000 > 0 then
                         begin
                              ans[ i ] := 'M' ;
                              inc( i ) ;
                              dif := dif - 1000 ;
                         end else if dif div 100 > 0 then
                                  begin
                                     if dif div 100 = 9 then
                                     begin
                                          ans[ i ] := 'C' ;
                                          ans[ i+1 ] := 'M' ;
                                          i := i + 2 ;
                                          dif := dif - 900 ;
                                     end ;
                                     if dif div 100 = 4 then
                                     begin
                                          ans[ i ] := 'C' ;
                                          ans[ i+1 ] := 'D' ;
                                          i := i + 2 ;
                                          dif := dif - 400 ;
                                     end ;
                                     if ( dif div 100 >=5 ) and ( dif div 100 < 9 ) then
                                     begin
                                          ans[ i ] := 'D' ;
                                          inc( i ) ;
                                          dif := dif - 500 ;
                                     end ;
                                     if ( dif div 100 >=1 ) and ( dif div 100 < 4 ) then
                                     begin
                                          ans[ i ] := 'C' ;
                                          inc( i ) ;
                                          dif := dif - 100 ;
                                     end ;
                                  end else if dif div 10 > 0 then
                                           begin
                                                if dif div 10 = 9 then
                                                begin
                                                     ans[ i ] := 'X' ;
                                                     ans[ i+1 ] := 'C' ;
                                                     i := i+2 ;
                                                     dif := dif - 90 ;
                                                end ;
                                                if dif div 10 = 4 then
                                                begin
                                                     ans[ i ] := 'X' ;
                                                     ans[ i+1 ] := 'L' ;
                                                     i := i+2 ;
                                                     dif := dif - 40 ;
                                                end ;
                                                if ( dif div 10 >=5 ) and ( dif div 10 < 9 ) then
                                                begin
                                                     ans[ i ] := 'L' ;
                                                     inc( i ) ;
                                                     dif := dif - 50 ;
                                                end ;
                                                if ( dif div 10 >= 1 ) and ( dif div 10 < 4 ) then
                                                begin
                                                     ans[ i ] := 'X' ;
                                                     inc( i ) ;
                                                     dif := dif - 10 ;
                                                end ;
                                           end else if dif > 0 then
                                                    begin
                                                         if dif = 9 then
                                                         begin
                                                              ans[ i ] := 'I' ;
                                                              ans[ i+1 ] := 'X' ;
                                                              i := i+2 ;
                                                              dif := dif - 9 ;
                                                         end ;
                                                         if dif = 4 then
                                                         begin
                                                              ans[ i ] := 'I' ;
                                                              ans[ i+1 ] := 'V' ;
                                                              i := i+2 ;
                                                              dif := dif - 4 ;
                                                         end ;
                                                         if ( dif >= 5 ) and ( dif < 9 ) then
                                                         begin
                                                              ans[ i ] := 'V' ;
                                                              inc( i ) ;
                                                              dif := dif - 5 ;
                                                         end ;
                                                         if ( dif >= 1 ) and ( dif < 4 ) then
                                                         begin
                                                              ans[ i ] := 'I' ;
                                                              inc( i ) ;
                                                              dif := dif - 1 ;
                                                         end ;
                                                    end ;
                         for cnt1 := 1 to i-1 do
                         write( ans[ cnt1 ] ) ;
                         writeln ;
                    end ;
          end ;
     end ;
end.

找了很久也不知錯處在哪... code很亂..煩請高手耐心看..

 
ZeroJudge Forum