0
点赞
收藏
分享

微信扫一扫

华师复试 D 罗马数字

一ke大白菜 2022-04-03 阅读 47
c++模拟

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
string change(int digit,int unit){    	//digit位数上的数字 unit第几位(unit=1表示个位)
	string s;
	if(digit>0&&digit<4) {
		for(int i=0;i<digit;i++){
			if(unit==1) s+='I';
			else if(unit==2) s+='X';
			else if(unit==3) s+='C';
			else if(unit==4) s+='M';
		}
	}else if(digit==4){
		if(unit==1) s="IV";
		else if(unit==2) s="XL";
		else if(unit==3) s="CD";
	}else if(digit==5){
		if(unit==1) s="V";
		else if(unit==2) s="L";
		else if(unit==3) s="D";
	}else if(digit>5&&digit<9){
		if(unit==1) s="V";
		else if(unit==2) s="L";
		else if(unit==3) s="D";
		for(int i=5;i<digit;++i){
			if(unit==1) s=s+"I";
			else if(unit==2) s=s+"X";
			else if(unit==3) s=s+"C";
		}
	}else if(digit==9){
		if(unit==1) s="IX";
		else if(unit==2) s="XC";
		else if(unit==3) s="CM";
	}
	return s;
}
int main(){
	int num,unit=1;
	cin>>num;
	string res;
	while(num>0){
		int temp=num%10;
		res=change(temp,unit++)+res;
		num=num/10;
	}
	cout<<res;
	return 0;
}
举报

相关推荐

0 条评论