codewar Roman Numerals Encoder

Create a function taking a positive integer as its parameter and returning a string containing the Roman Numeral representation of that integer.

Modern Roman numerals are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. In Roman numerals 1990 is rendered: 1000=M, 900=CM, 90=XC; resulting in MCMXC. 2008 is written as 2000=MM, 8=VIII; or MMVIII. 1666 uses each Roman symbol in descending order: MDCLXVI


string solution(int number){
  // convert the number to a roman numeral
    int  values [] ={1000,900,500,400,100,90,50,40,10,9,5,4,1};
        string symbol [13] ={"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
        int currentNumber=number;
        string str="";
        for(int i=0;i<13;i++)
        {
            if(currentNumber>=values[i])
            {
                int i_count=currentNumber/values[i];
                currentNumber=currentNumber%values[i];
                for(int j=0;j<i_count;j++)
                {
                    str+=symbol[i];
                }
            }
        }
        return str;
}


好几年的Qt 纯C++ 感觉获取个数组长度都需要百度,最后还是这样简单 人生苦短,何必C++,思路源于提成计算,缴税计算,对于4,9的问题,不知道有没有更好的办法?




阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页