题目:

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. Application Designer Security

    This wiki page covers how to manage and restrict Application Designer security through permission li ...

  2. OpenFileDialog使用方法

    OpenFileDialog基本属性 AddExtension 控制是否将扩展名自动添加到文件名上 CheckFileExists 指示用户指定不存在的文件时是否显示警告 CheckPathExist ...

  3. PHPExcel操作sae的storage上的文件

    在用PHPexcel操作excel的时候,在本地是好使的,但是把代码部署到sae就不好使了.会遇到如下问题: 文件的操作被拒绝. 这个原因就是sae上的应用文件是不允许改动的.sae提供的文件方案是使 ...

  4. Entity Framework中的多个库操作批量提交、事务处理

    在Entity Framework 中使用SaveChanges()是很频繁的,单次修改或删除数据后调用SaveChanges()返回影响记录数. 要使用批量修改或者批量删除数据,就需要SaveCha ...

  5. 使APP消除上方手机消息提示栏(显示WIFI,信号格那栏)消失的方法

    public void toggleFullscreen(boolean fullScreen) { // fullScreen为true时全屏,否则相反 WindowManager.LayoutPa ...

  6. 16)JAVA实现回调(Android,Swing中各类listener的实现)

           熟悉MS-Windows和X Windows事件驱动设计模式的开发人员,通常是把一个方法的指针传递给事件源,当某一事件发生时来调用这个方法(也称为"回调").Java ...

  7. WCF全面解析第一章 WCF 简介

    1.WCF中的 "A","B","C" 介绍 我们先看个生活中的例子,某一天,公司的领导让你去送一份合同文件,送文件的过程你可以选择的交通方 ...

  8. python内建函数-数字相关

    本篇对于数字有关的内置函数进行总结. 数字包括 int() , long() , float() , complex() ,这些函数都能够用来进行数值类型的转换.同时这些函数也接受字符串参数,返回字符 ...

  9. Moses创建一个翻译系统的基本过程记录,以后会按照每个过程详细说明,并给出每个步骤的参数说明

    软件需求: 首先你必须要有Moses(废话哈哈).然后要有GIZA++用作词对齐(traning-model.perl的时候会用到).IRSTLM产生语言模型 大致步骤: 大体的步骤如下: 准备Par ...

  10. angularjs2 学习笔记(五) http服务

    angular2的http服务是用于从后台程序获取或更新数据的一种机制,通常情况我们需要将与后台交换数据的模块做出angular服务,利用http获取更新后台数据,angular使用http的get或 ...