a013.
羅馬數字
--
NPSC 模擬試題
| From: [140.132.97.65] |
發表日期
:
2012-11-14 20:02
#include<iostream>
#include<cmath>
using namespace std;
int number(char);
int tonum(int *,int,int);
int torome(int);
int main(){
char *A=new char,*B=new char;
cin>>A>>B;
while(*A!='#'){
int *a=new int,*b=new int;
int aa=0,bb=0;//字元數
for(int i=0;*(A+i)!='\0';i++){*(a+i)=number(*(A+i));aa++;}
for(int i=0;*(B+i)!='\0';i++){*(b+i)=number(*(B+i));bb++;}
for(int i=0;i<aa && i<bb;i++)cout<<*(a+i)<<" "<<*(b+i)<<endl;
int tmp_a=0,tmp_b=0;
tmp_a=tonum(a,aa,tmp_a);
tmp_b=tonum(b,bb,tmp_b);
int final=tmp_a - tmp_b;
torome(final);
cin>>A>>B;
}
return 0;
}
int number(char a){
switch(a){
case 'I':return 1;break;
case 'V':return 5;break;
case 'X':return 10;break;
case 'L':return 50;break;
case 'C':return 100;break;
case 'D':return 500;break;
case 'M':return 1000;break;
}
}
int tonum(int *a,int aa,int tmp){
for(int i=0;i<aa;i++){
if(*(a+i)<*(a+i+1) && *(a+i)>0 && *(a+i+1)<4000){tmp+=(*(a+i+1)-*(a+i));i++;}
else tmp+=*(a+i);
}
return tmp;
}
int torome(int a){
for(int i=1000;i>=1;i/=10){
if(i==1000)switch(a/i){
case 1:cout<<"M";continue;
case 2:cout<<"MM";continue;
case 3:cout<<"MMM";continue;
case 4:cout<<"MMMM";continue;
}
else if(i==100)switch(a/i){
case 1:cout<<"C";continue;
case 2:cout<<"CC";continue;
case 3:cout<<"CCC";continue;
case 4:cout<<"CD";continue;
case 5:cout<<"D";continue;
case 6:cout<<"DC";continue;
case 7:cout<<"DCC";continue;
case 8:cout<<"DCCC";continue;
case 9:cout<<"CM";continue;
}
else if(i==10)switch(a/i){
case 1:cout<<"X";continue;
case 2:cout<<"XX";continue;
case 3:cout<<"XXX";continue;
case 4:cout<<"XL";continue;
case 5:cout<<"L";continue;
case 6:cout<<"LX";continue;
case 7:cout<<"LXX";continue;
case 8:cout<<"LXXX";continue;
case 9:cout<<"XC";continue;
}
else switch(a/i){
case 0:cout<<"ZERO";continue;
case 1:cout<<"I";continue;
case 2:cout<<"II";continue;
case 3:cout<<"III";continue;
case 4:cout<<"IV";continue;
case 5:cout<<"V";continue;
case 6:cout<<"VI";continue;
case 7:cout<<"VII";continue;
case 8:cout<<"VIII";continue;
case 9:cout<<"IX";continue;
}
}
return 0;
}