zoukankan      html  css  js  c++  java
  • LeetCode题解(12)--Integer to Roman

    https://leetcode.com/problems/integer-to-roman/

    原题:

    Given an integer, convert it to a roman numeral.

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

    思路:

    掌握罗马数字规则并实现即可。(具体规则见 LeetCode(13):Roman to Integer)

    我的AC代码:

     1 class Solution {
     2 public:
     3     string intToRoman(int num) {
     4         string in[] = {"I","V","X","L","C","D","M"};
     5         int t,i=0,j=0;
     6         string res="",tmp="";
     7        // cout<<(tmp += in[0]+in[1]);
     8         while(num>0){
     9             t=num%10;
    10             switch (t){
    11                 case 0: break;
    12                 case 1: tmp = in[i]; break;
    13                 case 2: tmp = in[i]+in[i]; break; 
    14                 case 3: tmp = in[i]+in[i]+in[i]; break;
    15                 case 4: tmp = in[i]+in[i+1]; break;
    16                 case 5: tmp = in[i+1]; break;
    17                 case 6: tmp = in[i+1]+in[i]; break;
    18                 case 7: tmp = in[i+1]+in[i]+in[i]; break;
    19                 case 8: tmp = in[i+1]+in[i]+in[i]+in[i]; break;
    20                 case 9: tmp = in[i]+in[i+2]; break;
    21             }
    22             i += 2;
    23        //     cout<<in[1]+in[0]+in[0]+in[0]<<"  "<<tmp<<endl;
    24             res = tmp +res;
    25             num=num/10;
    26             tmp="";
    27             }
    28             cout<< res;
    29         return res;
    30     }
    31 };
  • 相关阅读:
    js模板引擎
    浮点数正则表达式
    DbContext SQLite配置文件
    JS中的HTML片段
    WPF 使用HttpListener搭建本地web服务器
    C#调用Windows(8/10)自带的虚拟键盘
    SQLSERVER 设置默认值
    SQLSERVER存储过程基本语法
    MSSQL存储过程
    WPF手动绑定事件
  • 原文地址:https://www.cnblogs.com/aezero/p/4548875.html
Copyright © 2011-2022 走看看