整数转换成罗马数字

时间限制:1000ms 内存限制:65536K

给定一个整数 num,将整数转换成罗马数字。

如 1,2,3,4,5对应的罗马数字分别为IIIIIIIVV等。

输入格式

第一行输入一个整数 num(1  num 3999)

输出格式

输出 num 对应的罗马数字。

样例输入

123

样例输出

CXXIII

总结:

一道简单题,不过思路很重要。刚开始没多想直接开始模拟罗马数字的规则,写了20分钟都没过,总是有问题。后来发现完全没必要管他语法。直接用一个二维字符指针数组把个十百千位可能用到的数全部存起来到时候直接用就行了。

代码:

#include <iostream>
#include <cstdio>

using namespace std;

char* board[4][10] = {
	{
		"","I","II","III","IV","V","VI","VII","VIII","IX"
	},
	{
		"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC"
	},
	{
		"","C","CC","CCC","CD","D","DC","DCC","DCCC","CM"
	},
	{
		"","M","MM","MMM"//这道题最多到3000,如果需要可以扩展到9000
	}
};

void Re(int N){
	string re;
	re += board[3][N/1000 % 10];
	re += board[2][N/100 % 10];
	re += board[1][N/10 % 10];
	re += board[0][N % 10];
	cout<<re<<endl; 
}

int main(){
	
	int N;
	while(cin>>N)
	Re(N);
	
	return 0;
} 

posted @ 2018-04-11 17:43  Assassin_poi君  阅读(237)  评论(0编辑  收藏  举报