leetcode第12题--Integer to Roman
Problem:
Given an integer, convert it to a roman numeral.
Input is guaranteed to be within the range from 1 to 3999.
把阿拉伯数字转换为罗马数字输出。百度一下对应的 I V X L C D M,代表1,5,10,50,100,500,1000 然后写一个子函数,输入数字和相应的位数级别,如个位为level 1,千为4.因为最多不会超过四千。所以可以如下。注意了,string用法很好,直接加就可以。
class Solution {
private:
string corRoman(int val, int level)
{
string base, media, large;
string s = "";
if (level == ) // 个位
{
base = "I";
media = "V";
large = "X";
}
else if (level == )
{
base = "X";
media = "L";
large = "C";
}
else if (level == )
{
base = "C";
media = "D";
large = "M";
}
else
{
base = "M";
}
if (val == )
return "";
if( val < )
{
for ( int i = ; i < val; i++)
s += base;
return s;
}
if (val == )
{
return base + media;
}
if (val < )
{
s = media;
for (int i = ; i < val; i++)
s+=base;
return s;
}
return base + large;
}
public:
string intToRoman(int num)
{
string s;
int a;
for (int i = ; i > ; i-- )
{
a = num/pow(,i - );
num %= (int)pow(, i - );
s+=corRoman(a,i);
}
return s;
}
};
这样就Accept了。
leetcode第12题--Integer to Roman的更多相关文章
- 【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(12)Integer to Roman
题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...
- LeetCodeOJ刷题之12【Integer to Roman】
Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...
- LeetCode第[13]题(Java):Roman to Integer
题目:罗马数字转换 题目难度:easy 题目内容:Roman numerals are represented by seven different symbols: I, V, X, L, C, D ...
- leetcode解决问题的方法||Integer to Roman问题
problem: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range ...
- LeetCode:12. Roman to Integer (Easy)
1. 原题链接 https://leetcode.com/problems/roman-to-integer/description/ 2. 题目要求 (1)将罗马数字转换成整数:(2)范围1-399 ...
- [LeetCode] 12. Integer to Roman 整数转化成罗马数字
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 ...
- [LeetCode] Integer to Roman 整数转化成罗马数字
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...
- [LeetCode][Python]Integer to Roman
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com'https://oj.leetcode.com/problems/integer ...
随机推荐
- BZOJ 3362 POJ 1984 Navigation Nightmare 并与正确集中检查
标题效果:一些养殖场是由一些南北或东西向的道路互连. 镶上在不断的过程中会问两个农场是什么曼哈顿的距离,假设现在是不是通信.那么输出-1. 思维:并与正确集中检查,f[i]点i至father[i]距离 ...
- [ACM] ZOJ 3816 Generalized Palindromic Number (DFS,暴力枚举)
Generalized Palindromic Number Time Limit: 2 Seconds Memory Limit: 65536 KB A number that will ...
- poj3903 Stock Exchange(最长上升子序列)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:id=3903">http://poj.org/problem?id=3903 Descrip ...
- 学生表sid,sname,结果表cid,cname,学生成绩表sid,cid,cscore,最高要求的分数输出候补课程专门命名
--1.建表SQL: --学生表: -- Createtable createtable STUDENT ( SID NUMBERnotnull, SNAME NVARCHAR2) ) table ...
- jsRender模板引擎
jsRender模板引擎 上一篇最后提到了模板,并尝试自己编写一个最简单版本:有些朋友可能用过 jqtmpl,这是一个基于jquery的模板引擎,不过它已经不再更新了,而且据说渲染速度比较慢.这里介绍 ...
- 《R实战》读书笔记三
第二章 创建数据集 本章概要 1探索R数据结构 2使用数据编辑器 3数据导入 4数据集标注 本章所介绍内容概括例如以下. 两个方面的内容. 方面一:R数据结构 方面二:进入数据或者导入数据到数据结构 ...
- (大数据工程师学习路径)第一步 Linux 基础入门----简单的文本处理
介绍 这一节我们将介绍这几个命令tr(注意不是tar),col,join,paste.实际这一节是上一节关于能实现管道操作的命令的延续,所以我们依然将结合管道来熟悉这些命令的使用. 一.常用的文本处理 ...
- Spring MVC异常处理详解(转)
下图中,我画出了Spring MVC中,跟异常处理相关的主要类和接口. 在Spring MVC中,所有用于处理在请求映射和请求处理过程中抛出的异常的类,都要实现HandlerExceptionReso ...
- 使用 CodeIgniter 框架快速开发 PHP 应用(四)
原文:使用 CodeIgniter 框架快速开发 PHP 应用(四) 使用 CI 简化数据库开发你学习CI 是因为你想要使编程更容易和更有生产力.这一章讲述CI的Active Record类. 如果C ...
- 十天学Linux内核之第五天---有关Linux文件系统实现的问题
原文:十天学Linux内核之第五天---有关Linux文件系统实现的问题 有时间睡懒觉了,却还是五点多醒了,不过一直躺倒九点多才算起来,昨晚一直在弄飞凌的嵌入式开发板,有些问题没解决,自己电脑系统的问 ...