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

     1 class Solution {
     2 public:
     3     string intToRoman(int num) {
     4         const static char* Roman = "IVXLCDM";
     5         string ret;
     6         for (int base = 0; num; num /= 10, base += 2) {
     7             int x = num % 10;
     8             switch (x) {
     9                 case 1: case 2: case 3:
    10                     ret = string(x, Roman[base]) + ret;
    11                     break;
    12                 case 4:
    13                     ret = Roman[base+1] + ret;
    14                     ret = Roman[base] + ret;
    15                     break;
    16                 case 5:
    17                     ret = Roman[base+1] + ret;
    18                     break;
    19                 case 6: case 7: case 8:
    20                     ret = string(x-5, Roman[base]) + ret;
    21                     ret = Roman[base+1] + ret;
    22                     break;
    23                 case 9:
    24                     ret = Roman[base+2] + ret;
    25                     ret = Roman[base] + ret;
    26                     break;
    27                 default:
    28                     break;
    29             }
    30         }
    31         return ret;
    32     }
    33 };
  • 相关阅读:
    C#
    C#
    C#
    C#
    C#
    C#
    系统工具
    远程登录
    文件传输服务
    软件安装
  • 原文地址:https://www.cnblogs.com/tanghulu321/p/3074956.html
Copyright © 2011-2022 走看看