题目:

Given an integer, convert it to a roman numeral.

Input is guaranteed to be within the range from 1 to 3999.

代码:

class Solution {
public:
string intToRoman(int num) {
if ( num< ) return NULL;
const int size = ;
std::string symbol_ori[size] = {"M","D","C","L","X","V","I"};
int value_ori[size] = {,,,,,,};
// gain extra symbol and value pair
std::vector<std::string> symbol_extra;
std::vector<int> value_extra;
for ( int i = ; i < size-; ++i ){
symbol_extra.push_back(symbol_ori[i]);
value_extra.push_back(value_ori[i]);
if ( !(i & ) ){
symbol_extra.push_back(symbol_ori[i+]+symbol_ori[i]);
value_extra.push_back(value_ori[i]-value_ori[i+]);
}
else{
symbol_extra.push_back(symbol_ori[i+]+symbol_ori[i]);
value_extra.push_back(value_ori[i]-value_ori[i+]);
}
}
symbol_extra.push_back(symbol_ori[size-]);
value_extra.push_back(value_ori[size-]); std::string result;
for ( size_t i = ; i < symbol_extra.size(); ++i )
{
int k = num / value_extra[i];
while ( k )
{
result += symbol_extra[i];
k--;
}
num = num % value_extra[i];
}
return result;
}
};

tips:

根据罗马数字进位的特殊规则,预先补上不能正常进位的symbol和value,这样代码可以非常consice

================================================

第二次过这道题,重写一遍加深印象。

class Solution {
public:
string intToRoman(int num) {
if ( num< ) return NULL;
const int size = ;
string symbol_ori[size] = {"M","D","C","L","X","V","I"};
int value_ori[size] = {,,,,,,};
vector<string> symbol_extra;
vector<int> value_extra;
for ( int i=; i<size-; ++i ){
symbol_extra.push_back(symbol_ori[i]);
value_extra.push_back(value_ori[i]);
if ( !( & i) )
{
symbol_extra.push_back(symbol_ori[i+]+symbol_ori[i]);
value_extra.push_back(value_ori[i]-value_ori[i+]);
}
else
{
symbol_extra.push_back(symbol_ori[i+]+symbol_ori[i]);
value_extra.push_back(value_ori[i]-value_ori[i+]);
}
}
symbol_extra.push_back(symbol_ori[size-]);
value_extra.push_back(value_ori[size-]);
string ret = "";
for ( int i=; i<value_extra.size(); ++i )
{
int count = num / value_extra[i];
while ( count--> ) ret = ret + symbol_extra[i];
num = num % value_extra[i];
}
return ret;
}
};

【Integer To Roman】cpp的更多相关文章

  1. LeetCodeOJ刷题之12【Integer to Roman】

    Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be within t ...

  2. 【First Missing Positive】cpp

    题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...

  3. 【Merge Sorted Array】cpp

    题目: Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Not ...

  4. 【Count and Say】cpp

    题目: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111 ...

  5. hdu 4740【模拟+深搜】.cpp

    题意: 给出老虎的起始点.方向和驴的起始点.方向.. 规定老虎和驴都不会走自己走过的方格,并且当没路走的时候,驴会右转,老虎会左转.. 当转了一次还没路走就会停下来.. 问他们有没有可能在某一格相遇. ...

  6. 【Power of Two】cpp

    题目: Given an integer, write a function to determine if it is a power of two. 代码: class Solution { pu ...

  7. 【Spiral Matrix II】cpp

    题目: Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. ...

  8. 【Search Insert Position 】cpp

    题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...

  9. 【Insertion Sorted List】cpp

    题目: Sort a linked list using insertion sort. 代码: /** * Definition for singly-linked list. * struct L ...

随机推荐

  1. nginx + php +mysql (适配thinkphp)

    Nginx 单机配置 http://tengine.taobao.org/book/index.html (taobao book) http://ubuntuhandbook.org/index.p ...

  2. CentOS学习笔记--SCSI 设备热插拔

    CentOS学习笔记--SCSI 设备热插拔 处于运行中的服务器,因业务要求也许不允许重启机器,而新添加的SCSI设备(主要是硬盘)如何实现热插拔呢? 首先需要查看一下设备: #cat /proc/s ...

  3. 软件工程 speedsnail 冲刺4

    2015-5-8 完成任务:学习了黑马android教学视频7.8.9集,对布局和计划做了调整: 遇到问题: 问题1 异常 Warning: Activity not started, its cur ...

  4. C#读取xlsx文件Excel2007

    读取Excel 2007的xlsx文件和读取老的.xls文件是一样的,都是用Oledb读取,仅仅连接字符串不同而已. 具体代码实例: public static DataTable GetExcelT ...

  5. 如何消除选定TextBox后的光标但又不失去焦点。

    情景描述: 选择TextBox里的内容 Name:textTile 但是没有光标. 相关实现代码: [DllImport("user32", EntryPoint = " ...

  6. Python初学者笔记:打印出斐波那契数列的前10项

    问题:斐波那契数列(意大利语: Successione di Fibonacci),又称黄金分割数列.费波那西数列.费波拿契数.费氏数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.- ...

  7. 8.python中的数字

    python中数字对象的创建如下, a = 123 b = 1.23 c = 1+1j 可以直接输入数字,然后赋值给变量. 同样也可是使用类的方式: a = int(123) b = float(1. ...

  8. C语言如何 实现 下雪效果

    题外话  前言 1.本文主要围绕 如何 在 控制台上 下起 一场 只有自己能看见的雪 2.是个简易跨平台的,主要是C语言 3.动画 采用 1s 40帧, 雪花具有 x轴速度和y轴速度 4.比较简单,可 ...

  9. OracleINSERT提示IGNORE_ROW_ON_DUPKEY_INDEX

    OracleINSERT提示IGNORE_ROW_ON_DUPKEY_INDEX insert提示IGNORE_ROW_ON_DUPKEY_INDEX 在 insert into table a() ...

  10. 【转】Oracle中如何用一条SQL快速生成10万条测试数据

    转自http://blog.csdn.net/welken/article/details/4971887   做数据库开发或管理的人经常要创建大量的测试数据,动不动就需要上万条,如果一条一条的录入, ...