zoukankan      html  css  js  c++  java
  • Leetcode 12. Integer to Roman(打表,水)

    12. Integer to Roman
    Medium

    Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

    Symbol       Value
    I             1
    V             5
    X             10
    L             50
    C             100
    D             500
    M             1000

    For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

    Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

    • I can be placed before V (5) and X (10) to make 4 and 9. 
    • X can be placed before L (50) and C (100) to make 40 and 90. 
    • C can be placed before D (500) and M (1000) to make 400 and 900.

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.

    Example 1:

    Input: 3
    Output: "III"

    Example 2:

    Input: 4
    Output: "IV"

    Example 3:

    Input: 9
    Output: "IX"

    Example 4:

    Input: 58
    Output: "LVIII"
    Explanation: L = 50, V = 5, III = 3.
    

    Example 5:

    Input: 1994
    Output: "MCMXCIV"
    Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.


     1 class Solution {
     2 public:
     3     string bit(int i, int id){
     4         string s;
     5         if(id == 4){
     6             if(i==1) s = "M";
     7             else if(i==2) s = "MM";
     8             else if(i==3) s = "MMM";
     9         }
    10         else if(id==3){
    11             if(i==1) s = "C";
    12             else if(i==2) s = "CC";
    13             else if(i==3) s = "CCC";
    14             else if(i==4) s = "CD";
    15             else if(i==5) s = "D";
    16             else if(i==6) s = "DC";
    17             else if(i==7) s = "DCC";
    18             else if(i==8) s = "DCCC";
    19             else if(i==9) s = "CM";
    20             else s = "";
    21         }
    22         else if(id==2){
    23             if(i==1) s = "X";
    24             else if(i==2) s = "XX";
    25             else if(i==3) s = "XXX";
    26             else if(i==4) s = "XL";
    27             else if(i==5) s = "L";
    28             else if(i==6) s = "LX";
    29             else if(i==7) s = "LXX";
    30             else if(i==8) s = "LXXX";
    31             else if(i==9) s = "XC";
    32             else s = "";
    33         }
    34         else if(id==1){
    35             if(i==1) s = "I";
    36             else if(i==2) s = "II";
    37             else if(i==3) s = "III";
    38             else if(i==4) s = "IV";
    39             else if(i==5) s = "V";
    40             else if(i==6) s = "VI";
    41             else if(i==7) s = "VII";
    42             else if(i==8) s = "VIII";
    43             else if(i==9) s = "IX";
    44             else s = "";
    45         }
    46         return s;
    47     }
    48     string intToRoman(int num) {
    49         string ans;
    50         ans += bit(num/1000,4);
    51         num %=1000;
    52         ans += bit(num/100,3);
    53         num %=100;
    54         ans += bit(num/10,2);
    55         num %=10;
    56         ans += bit(num,1);
    57         return ans;
    58     }
    59     
    60 };
  • 相关阅读:
    S3C2440的LCD虚拟显示测试
    arm-linux-gcc编译器测试
    韦东山教程ARM的时钟设置出现的问题及其解决方法
    程序在nor flash中真的可以运行吗?
    存储器的速度
    程序测试的方法
    对编程的一些思考

    [算法题] 字节流解析
    [C/C++]函数指针和函数分发表
  • 原文地址:https://www.cnblogs.com/shanyr/p/11437881.html
Copyright © 2011-2022 走看看