leetcode intToRoman
来源:互联网 发布:smartgit linux 破解 编辑:程序博客网 时间:2024/05/20 18:16
#include<iostream>
#include<unordered_map>
#include<string>
using namespace std;
string intToRoman(int num)
{
if(num>3999||num<1)
return string("the num is out of range");
unordered_map<int,string> map;
map[1000]="M";
map[900]="CM";
map[500]="D";
map[400]="CD";
map[100]="C";
map[90]="XC";
map[50]="L";
map[40]="XL";
map[10]="X";
map[9]="IX";
map[5]="V";
map[4]="IV";
map[1]="I";
string result;
unordered_map<int,string>::iterator iter=map.begin();
while(num>0)
{
int i=iter->first;
int count=num/i;
num%=i;
for(;count>0;--count)
{
result+=map[i];
}
++iter;
}
return result;
}
int main()
{
cout<<intToRoman(1000)<<endl;
cout<<intToRoman(900)<<endl;
cout<<intToRoman(500)<<endl;
cout<<intToRoman(400)<<endl;
cout<<intToRoman(100)<<endl;
cout<<intToRoman(90)<<endl;
cout<<intToRoman(50)<<endl;
cout<<intToRoman(40)<<endl;
cout<<intToRoman(10)<<endl;
cout<<intToRoman(9)<<endl;
cout<<intToRoman(5)<<endl;
cout<<intToRoman(4)<<endl;
cout<<intToRoman(1)<<endl;
cout<<intToRoman(1234)<<endl;
return 0;
}
#include<unordered_map>
#include<string>
using namespace std;
string intToRoman(int num)
{
if(num>3999||num<1)
return string("the num is out of range");
unordered_map<int,string> map;
map[1000]="M";
map[900]="CM";
map[500]="D";
map[400]="CD";
map[100]="C";
map[90]="XC";
map[50]="L";
map[40]="XL";
map[10]="X";
map[9]="IX";
map[5]="V";
map[4]="IV";
map[1]="I";
string result;
unordered_map<int,string>::iterator iter=map.begin();
while(num>0)
{
int i=iter->first;
int count=num/i;
num%=i;
for(;count>0;--count)
{
result+=map[i];
}
++iter;
}
return result;
}
int main()
{
cout<<intToRoman(1000)<<endl;
cout<<intToRoman(900)<<endl;
cout<<intToRoman(500)<<endl;
cout<<intToRoman(400)<<endl;
cout<<intToRoman(100)<<endl;
cout<<intToRoman(90)<<endl;
cout<<intToRoman(50)<<endl;
cout<<intToRoman(40)<<endl;
cout<<intToRoman(10)<<endl;
cout<<intToRoman(9)<<endl;
cout<<intToRoman(5)<<endl;
cout<<intToRoman(4)<<endl;
cout<<intToRoman(1)<<endl;
cout<<intToRoman(1234)<<endl;
return 0;
}
0 0
- leetcode intToRoman
- intToRoman
- Problem12:intToRoman
- intToRoman算法题
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- 还是畅通工程
- Windows如何用PL/SQL Developer远程连接Oracle数据库
- 两个连续独立随机变量的商的概率密度函数
- Java实现排序算法之堆排序
- centos 安装Sublime
- leetcode intToRoman
- java二叉树非递归之中序遍历
- vs2008 mfc工程界面显示win98风格问题
- SSL常见加密算法
- 网络编程系列之九 getopt()函数
- C++怎么判断一个类存在指定的函数名的函数
- 各种加解密算法比较
- Qt中如何写一个model
- akoj-1291-决战21点(C#)