罗马数字转化

IV=4

#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
char ss[1000];
int main()
{
    while(cin>>ss)
    {  int sum=0;;
        for(int i=0; i<strlen(ss); i++)
        {

            switch(ss[i])
            {
            case'M':
                sum+=1000;
                break;
            case'D':
                sum+=500;
                break;
            case'C':
                if(ss[i+1]=='D'||ss[i+1]=='M')
                    sum-=100;
                else sum+=100;
                break;
            case'L':
                sum+=50;
                break;
            case'X':
                if(ss[i+1]=='L'||ss[i+1]=='C')
                    sum-=10;
                else sum+=10;
                break;
            case'I':
                if(ss[i+1]=='X'||ss[i+1]=='V')
                    sum-=1;
                else sum+=1;
                break;
            }

        }
        cout<<sum<<endl;
    }
}

  6=VI

#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
    int  n;
    while(cin>>n)
    {
        char* one[15]= {"","I","II","III","IV","V","VI","VII","VIII","IX"};
        char* ten[15] = {"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"};
        char* hundreds[15] = {"", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"};
        char* thousand[5] = {"","M","MM","MMM"};
        cout<<thousand[n/1000];
        cout<<hundreds[n%1000/100];
        cout<<ten[n%100/10];
        cout<<one[n%10];
    }

}

  

 

posted @ 2016-07-22 13:17  799  阅读(214)  评论(0编辑  收藏  举报