• Integer to Roman——相当于查表法


    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等等表示的,其中
    I = 1;
    V = 5;
    X = 10;
    L = 50;
    C = 100;
    D = 500;
    M = 1000;
    接下来应该是V开始的重复,但是上面要加一个横线,表示对应数字的1000倍。
    个位应该是:I,II,III,IV,V,VI,VII,VIII,IX
    十位应该是:X,XX,XXX,XL,L,LX,LXX,LXXX,XC
    百位应该是:C,CC,CCC,CD,D,DC,DCC,DCCC,CM

    class Solution {
    public:
        string intToRoman(int num) {
             char c[10][10][10]={{"0","I","II","III","IV","V","VI","VII","VIII","IX"},{"0","X","XX","XXX","XL","L","LX"
            ,"LXX","LXXX","XC"},{"0","C","CC","CCC","CD","D",
                  "DC","DCC","DCCC","CM"},{"0","M","MM","MMM"}};
            int t=1;
            int tmp=num;
            string st;
            if(tmp/1000!=0) st+=c[3][tmp/1000];
            if((tmp%1000)/100!=0) st+=c[2][(tmp%1000)/100];
            if((tmp%100)/10!=0) st+=c[1][(tmp%100)/10];
            if(tmp%10!=0) st+=c[0][tmp%10];
            return st;
            
        }
    };
  • 相关阅读:
    Oracle8i安装问题
    c# 值類型與引用類型
    C# String轉成FontStyle
    为GridView“删除”列添加确认对话框(轉)
    c# 輸出字符編碼
    了解C#命名空間
    Asp.Net安全控件說明
    C# 数组学习
    c# 方法的參數傳遞機制
    c# 得到所有XmlElement節點的值
  • 原文地址:https://www.cnblogs.com/qiaozhoulin/p/4569555.html
Copyright © 2020-2023  润新知