zoukankan      html  css  js  c++  java
  • LeetCode-Integer to Roman

    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) {
            string ret;
            while(num>999){
                ret.append("M");
                num-=1000;
            }
            if(num>899){
                ret.append("CM");
                num-=900;
            }
            
            if(num>499){
               ret.append("D");
               num-=500;
            }
            if(num>399){
                ret.append("CD");
                num-=400;
            }
            while(num>99){
                ret.append("C");
                num-=100;
            }
            
            if(num>89){
               ret.append("XC");
               num-=90;
            }
            if(num>49){
                ret.append("L");
                num-=50;
            }
            if(num>39){
               ret.append("XL");
               num-=40;
            }
            while(num>9){
                ret.append("X");
                num-=10;
            }
            
             if(num>8){
                ret.append("IX");
                num-=9;
            }
            if(num>4){
                ret.append("V");
                num-=5;
            }
            if(num>3){
                ret.append("IV");
                num-=4;
            }
            while(num>0){
                ret.append("I");
                num-=1;
            }
            return ret;
        }
    };
    
  • 相关阅读:
    servlet
    过滤器
    拦截器
    logback
    hibernate(1)
    函数的关键字参数
    函数的不定长参数
    打印星形三角
    九九乘法表
    udp客户端收发数据流程
  • 原文地址:https://www.cnblogs.com/superzrx/p/3297907.html
Copyright © 2011-2022 走看看