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.

 

题目分析:

    看了半天,题目都没看懂,都不知道了roman numeral是什么东西.于是在网上查了下,才明白是个什么东东,表示很无语.

模拟题:

                    基本字符IVXLCDM
           相应的阿拉伯数字表示含义1510501005001000

1.相同的数字连写,所表示的数等于这些数字相加得到的数,如:III =3;

2.小的数字在大的数字的左边,则所表示的数等于大数减小的数得到的数.如:IV=4;iX=9;

3.小的数字在大的数字的右边,则所表示的数等于大数加上小的数得到的数.如:VI=6;XI=11;

代码:

class Solution{
public:
    void setDigit(string &res,int n,char a,char b,char c){
           if(n<4){
                 for(int i=0;i<n;i++){
                         res.push_back(a);
                }
           }
           
           else if(n==4){
                 res.push_back(a);
                 res.push_back(b);
           }
           
           else if(n==5){
                 res.push_back(b);
           }
           
           else if(n>5 && n<9){
                  res.push_back(b);
                  for(int i=5;i<n;i++){
                        res.push_back(a);
                  }
          }
          
          else if(n==9){
              res.push_back(a);
              res.push_back(c);
          }
    }
    
    string intToRoman(int num){
          int a1=(num/1000)%10,a2=(num/100)%10,a3=(num/10)%10,a4=num%10;
          setDigit(res,a1,'M','?','?');
          setDigit(res,a2,'C','D','M');
          setDigit(res,a3,'X','L','C');
          setDigit(res,a4,'I','V','X');
          return res;
    }
};

 参考别人写的...

转载于:https://www.cnblogs.com/sixue/p/4002349.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值