20-Integer to Roman-Leetcode
比较简单的思路:用map存放各个位的数字到罗马字符的映射
然后从个位依次遍历高位加上映射即可。
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
罗马表示法参看上一篇博文
#include <algorithm>
#include <iostream>
#include <vector>
#include <string>
#include <map>
#define IMIN numeric_limits<int>::min()
#define IMAX numeric_limits<int>::max()
#define FR(i,n) for(int i=0;i<n;i++)
#define CLC(x) memset(x,0,sizeof(x))
#define FILL(x,c) memset(x,c,sizeof(x))
using namespace std;
class Solution {
public:
string intToRoman(int num) {
map<int,string> mp1,mp2,mp3,mp4;
init1(mp1);init2(mp2);init3(mp3);init4(mp4);
string s;
int n=1;
while(num)
{
int tmp=num%10;
switch(n)
{
case 1:s=mp1[tmp]+s;break;
case 2:s=mp2[tmp]+s;break;
case 3:s=mp3[tmp]+s;break;
case 4:s=mp4[tmp]+s;break;
}
// cout<<s<<endl;
// cout<<num<<endl;
num = num/10;
n++;
}
return s;
}
void init1(map<int,string> &mp)
{
mp[0]="";
mp[1]=string("I");mp[2]=string("II");
mp[3]=string("III");mp[4]=string("IV");
mp[5]=string("V");mp[6]=string("VI");
mp[7]=string("VII");mp[8]=string("VIII");
mp[9]=string("IX");
}
void init2(map<int,string> &mp)
{
mp[0]="";
mp[1]=string("X");mp[2]=string("XX");
mp[3]=string("XXX");mp[4]=string("XL");
mp[5]=string("L");mp[6]=string("LX");
mp[7]=string("LXX");mp[8]=string("LXXX");
mp[9]=string("XC");
}
void init3(map<int,string> &mp)
{
mp[0]="";
mp[1]=string("C");mp[2]=string("CC");
mp[3]=string("CCC");mp[4]=string("CD");
mp[5]=string("D");mp[6]=string("DC");
mp[7]=string("DCC");mp[8]=string("DCCC");
mp[9]=string("CM");
}
void init4(map<int,string> &mp)
{
mp[1]=string("M");mp[2]=string("MM");
mp[3]=string("MMM");
}
};
int main()
{
Solution s;
int n;
while(cin>>n)
cout<<s.intToRoman(n)<<endl;
return 0;
}
20-Integer to Roman-Leetcode的更多相关文章
- Integer to Roman - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Integer to Roman - LeetCode 注意点 考虑输入为0的情况 解法 解法一:从大到小考虑1000,900,500,400,100,9 ...
- Integer to Roman -- LeetCode 012
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- Integer To Roman leetcode java
问题描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- integer to roman leetcode c++实现
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- 【LeetCode】12 & 13 - Integer to Roman & Roman to Integer
12 - Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be wit ...
- [LeetCode][Python]Integer to Roman
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...
- leetCode练题——12. Integer to Roman
1.题目 12. Integer to Roman Roman numerals are represented by seven different symbols: I, V, X, L, C, ...
- 【LeetCode】Roman to Integer & Integer to Roman
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
- 【leetcode】Integer to Roman
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
- 【leetcode】Integer to Roman & Roman to Integer(easy)
Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within t ...
随机推荐
- BUAA 2020 软件工程 提问回顾与个人总结
BUAA 2020 软件工程 提问回顾与个人总结 Author: 17373051 郭骏 项目 内容 这个作业属于哪个课程 2020春季计算机学院软件工程(罗杰 任健) 这个作业的要求在哪里 提问回顾 ...
- 状压dp学习笔记(紫例题集)
P3451旅游景点 Tourist Attractions 这个代码其实不算是正规题解的(因为我蒟蒻)是在我们的hzoj上内存限制324MIB情况下过掉的,而且经过研究感觉不太能用滚动数组,所以那这个 ...
- C语言基础知识总结大全
1.入门程序 #include <stdio.h> int main() { printf("Hello World!"); return 0; } 2.数据类型 数据 ...
- hdu 2201 熊猫阿波的故事(简单概率。。)
题意: 阿波上了飞机,飞机上有座位1,2,....,N.第i个乘客本应坐在第i个座位上. 可是阿波随便找了个座位就坐了下来,接下来大家也都随便找了个座位坐了下来. 问:第i个乘客坐到原座位的概率是多少 ...
- hdu 5095 Linearization of the kernel functions in SVM(模拟,分类清楚就行)
题意: INPUT: The input of the first line is an integer T, which is the number of test data (T<120). ...
- vue2强制刷新,解决页面不会重新渲染的问题
问题描述: 在使用Vue框架开发时,在函数中改变了页面中的某个值,在函数中查看是修改成功了,但在页面中没有及时刷新改变后的值: 解决: 运用 this.$forceUpdate(); //强制刷新, ...
- HTML bootstrap 模态对话框添加用户
HTML 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> ...
- Oracle 19c 单机
环境 vm虚拟机 双磁盘 操作系统 Oracle Linux 7.9 操作系统安装带图形 选择中文,注意不要新建用户 关闭防火墙 selinux 配置好IP 挂载系统盘镜像 修改主机名 配置hosts ...
- CLion 2021.2 debug报错 process exited with status -1 (attach failed (Not allowed to attach to process.
Clion 升级 2021.2 版本后 debug 报错: process exited with status -1 (attach failed (Not allowed to attach to ...
- python微服务
https://realpython.com/python-microservices-grpc/ https://github.com/saqibbutt/python-flask-microser ...