数据转换:输入一个数,0<数<1000;
1 2 3 4 5 6 7 8 9
I II III IV V VI VII VIII IX
10 20 30 40 50 60 70 80 90
X XX XXX XL L LX LXX LXXX XLL
100 200 300 400 500 600 700 800 900
C CC CCC CD D DC DCC DCCC CM
示例:
输入: 863
输出:DCCCLXIII
代码实现:
#include<iostream>
using namespace std;int main()
{ cout<<"请输入一个1~999之间的整数:"<<endl;
int m;
//对输入数据进行检查,如果输入数据不合要求则反复要求继续输入一数据,直到数据合法
do{
cin>>m;
if(m<0||m>999)
{ cout<<"输入数据错误,请重新输入。"<<endl;
}
}while(m<0||m>999);
int ge=0,shi=0,bai=0;
bai=(m/100); //取得输入数的百位数字
shi=(m/10)-bai*10; //取得输入数的十位数字
ge=m%100-shi*10; //取得输入数的个位数字
int array[3]; //定义一个数组存入该输入数的百,十,个
array[0]=bai*100;
array[1]=shi*10;
array[2]=ge;
void Change(int a[]);
Change(array);
return 0;
}void Change(int a[])
{
for(int i=0;i<3;i++)
switch(a[i])
{
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;
case 10:cout<<"X";break;
case 20:cout<<"XX";break;
case 30:cout<<"XXX";break;
case 40:cout<<"XL";break;
case 50:cout<<"L";break;
case 60:cout<<"LX";break;
case 70:cout<<"LXX";break;
case 80:cout<<"LXXX";break;
case 90:cout<<"XC";break;
case 100:cout<<"C";break;
case 200:cout<<"CC";break;
case 300:cout<<"CCC";break;
case 400:cout<<"CD";break;
case 500:cout<<"D";break;
case 600:cout<<"DC";break;
case 700:cout<<"DCC";break;
case 800:cout<<"DCCC";break;
case 900:cout<<"CM";break;
default:break;
}
}