#5348: 卡在第8個測資


st930312 (糖果)

學校 : 臺北市立大同高級中學
編號 : 11223
來源 : [211.23.129.205]
最後登入時間 :
2016-03-17 10:21:21
a013. 羅馬數字 -- NPSC 模擬試題 | From: [180.176.152.183] | 發表日期 : 2011-07-13 03:46

#include "iostream"
#include "string"


using namespace std;

int math(char a){

switch(a)
     {
     case 'M':
          return 1000;
          break;
     case 'D':
          return 500;
          break;
     case 'C':
          return 100;
          break;
     case 'L':
          return 50;
          break;
     case 'X':
          return 10;
          break;
     case 'V':
          return 5;
          break;
     case 'I':
          return 1;
          break;     
     }
}

void roma(char a,int b){

        switch(b){
               case 4:
                    switch(a){
                    case '1':
                         cout<<"M";
                    break;
                    case '2':
                         cout<<"MM";
                    break;
                    case '3':
                         cout<<"MMMM";
                    break;}

               break;
                    
               case 3:
                    switch(a){
                    case '1':
                         cout<<"C";
                    break;
                    case '2':
                         cout<<"CC";
                    break;
                    case '3':
                         cout<<"CCC";
                    break;
                    case '4':
                         cout<<"CD";
                    break;
                    case '5':
                         cout<<"D";
                    break;
                    case '6':
                         cout<<"DC";
                    break;
                    case '7':
                         cout<<"DCC";
                    break;
                    case '8':
                         cout<<"DCCC";
                    break;
                    case '9':
                         cout<<"CM";
                    break;}
               break;
               case 2:
                    switch(a){
                    case '1':
                         cout<<"X";
                    break;
                    case '2':
                         cout<<"XX";
                    break;
                    case '3':
                         cout<<"XXX";
                    break;
                    case '4':
                         cout<<"XL";
                    break;
                    case '5':
                         cout<<"L";
                    break;
                    case '6':
                         cout<<"LX";
                    break;
                    case '7':
                         cout<<"LXX";
                    break;
                    case '8':
                         cout<<"LXXX";
                    break;
                    case '9':
                         cout<<"XC";
                    break;}
               break;
               case 1:
                    switch(a){
                    case '1':
                         cout<<"I";
                    break;
                    case '2':
                         cout<<"II";
                    break;
                    case '3':
                         cout<<"III";
                    break;
                    case '4':
                         cout<<"IV";
                    break;
                    case '5':
                         cout<<"V";
                    break;
                    case '6':
                         cout<<"VI";
                    break;
                    case '7':
                         cout<<"VII";
                    break;
                    case '8':
                         cout<<"VIII";
                    break;
                    case '9':
                         cout<<"IX";
                    break;}
               break;
               }}


int main(){
string a,b;

while(cin>>a){
int  z=0,x=0,y=0;
if(a=="#")
break;
else{
     cin>>b;
     if(a=="ZERO"){
     cout<<b;continue;}
     else if(b=="ZERO"){
     cout<<a;continue;}
     
     int i=0,c=a.length(),d=b.length();
     int k[c],g[d];
     for(i=0;i<c;i++)
     k[i]=math(a[i]);
     
     for(i=0;i<c;i++)
     if(k[i]>=k[i+1]||k[i]==k[c-1])
     y=y+k[i];
     else
     y=y-k[i];
                                      
     for(i=0;i<d;i++)
     g[i]=math(b[i]);
     
     for(i=0;i<d;i++)
     if(g[i]>=g[i+1]||g[i]==g[d-1])
     x=x+g[i];
     else
     x=x-g[i];
     
     if(y>x)
     z=y-x;
     else if(y=x){
     cout<<"ZERO"<<endl;continue;}
     else
     z=x-y;
     
     char s[4]={z/1000+48,z%1000/100+48,z%1000%100/10+48,z%1000%100%10+48};
     
     for(i=0;i<4;i++)
     {
             roma(s[i],4-i);
             }
     
     }

cout<<endl;                               
}

return 0;
}

 

他說我的答案是zero

正確答案是xviii...........

 

不知道錯在哪= =

 
ZeroJudge Forum