[求助]如何转化阿拉伯数字和罗马数字?
|
|
4 | IV |
72 | LXXII |
148 | CXLVIII |
291 | CCXCI |
704 | DCCIV |
2008 | MMVIII |
1984 | MCMLXXXIV |
上图是罗马数字的表示,求各位大侠指点下,如何将自己输入的任意阿拉伯数字转化为罗马数字.这个程序的关键函数要怎么写?比如:
[此贴子已经被作者于2007-9-17 5:42:09编辑过]
|
|
4 | IV |
72 | LXXII |
148 | CXLVIII |
291 | CCXCI |
704 | DCCIV |
2008 | MMVIII |
1984 | MCMLXXXIV |
[此贴子已经被作者于2007-9-17 5:42:09编辑过]
#include <iostream>
#include <string>
using namespace std;
int arr1[]={1000,900,500,400,100,90,50,40,10,9,5,4,1};
string arr2[]={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
string covert2Roman(int n)
{
string s;
int i=0;
while(n){
if(n>=arr1[i]){
n-=arr1[i];
s+=arr2[i];
}
else {
i++;
}
}
return s;
}
int main()
{
int n;
while(cin>>n){
cout<<covert2Roman(n)<<endl;
}
}