- #include <iostream>
- #include <cstdlib>
- #include <cstring>
- #define N 7
- using namespace std ;
- string roman[N] = {"M", "D", "C", "L", "X", "V", "I"};
- int arabic[N] = {1000, 500, 100, 50, 10, 5, 1};
-
- int to_int(string s){
- int tmp =0 ;
- int m = 0 ;
- for(int i=0;i<s.length();i++){
- for(int j=0;j<N;j++){
- string c = s.substr(i,1) ;
- if( roman[j]== c){
- tmp += arabic[j];
- break;
- }
- }
- }
- return tmp ;
- }
- string to_roman(int ab){
- if(ab==0){
- return "ZERO";
- }
- string tmp ;
- int t ;
- for(int i=0;i<N;i+=2){
- t = ab/arabic[i];
- if(t==9||t==4){
- int les = arabic[i-2] - t*arabic[i];
- for(int j=0;j<N;j++){
- if(arabic[j]==les){
- tmp += roman[j];
- break;
- }
- }
- tmp+=roman[i-2];
- }else{
- if(t>4){
- tmp += roman[i-1];
- t-=5 ;
- }
- for(int z=0;z<t;z++){
- tmp+=roman[i];
- }
- }
- ab %= arabic[i];
- }
- return tmp ;
- }
- int main(){
-
- string s = "MM";
- string s2 = "II" ;
- int a ,b ;
- a = to_int(s);
- b = to_int(s2);
- cout << to_roman(a-b) ;
-
- cout << endl ;
- system("pause");
- }
複製代碼 |