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的问题,不知道有没有更好的办法?




Roman Numerals

10-11

DescriptionnnThe original system of writing numbers used by the early Romans was simple but cumbersome. Various letters were used to represent important numbers, and these were then strung together to represent other numbers with the values decreasing monotonically from left to right. The letters they used and the numbers that were represented are given in the following table. nnI 1 V 5nX 10 L 50nC 100 D 500nM 1000 nnThus 1993 was written as MDCCCCLXXXXIII. This system was then superseded by a partially place-oriented system, whereby if the above rule of decreasing values was broken, it meant that the immediately preceding (lower) value was deemed to be `negative' and was subtracted from the higher (out of place) value. In this system 1993 was usually written as MCMXCIII. There is still some controversy as to which letters could precede which other letters, but for the purposes of this problem we will assume the following restrictions: nnn1. nA letter from the left column can never appear more than three times in a row, and there can never be more than one other occurrence of that letter. nn2. nA letter from the right column can never appear more than once. nn3. nOnce a letter has been used in a `negative' position, all subsequent characters (apart from the one immediately following) may not be greater than that character. nThus we could write MXMIII for 1993 or CCXCIV for 294, however we could not write ILV for 54, nor could we write LIL for 99. Note that 299 could be written as CCXCIX or CCIC nnnnGiven a Roman sum, we can either interpret it as such or as an encoding of an Arabic sum. Thus V+V=X could be interpreted as an ambiguous encoding of an Arabic sum with V ∈ 1, 2, 3, 4 and X = 2 * V. Similarly, X+X=XX could be interpreted as a correct Roman sum but an impossible Arabic encoding (apart from the trivial encoding X = 0) and XX+XX=MXC as an incorrect Roman sum, but a valid encoding with M = 1, X = 9, and C = 8. nnWrite a program that will read in sums in Roman numerals and determine whether or not they are correct as Roman sums and also whether they are impossible, ambiguous or valid as Arabic encodings. Assume that zero will never appear on its own or as a leading digit, and that no two Roman numerals map onto the same Arabic digit. nInputnnInput will consist of a series of lines, each line consisting of an apparent Roman sum, i.e. a valid Roman number, a plus sign (+), another valid Roman number, an equal sign (=) and another valid Roman number. No Roman number will contain more than 9 letters. The file will be terminated by a line consisting of a single #.nOutputnnOutput will consist of a series of lines, one for each line of the input, and each containing two words. The first word will be one of (Correct, Incorrect) depending on whether the Roman sum is or is not correct. The second word will be separated from the first by exactly one space and will be one of the set (impossible, ambiguous, valid) depending on the Arabic sum.nSample InputnnV+V=XnX+X=XXnXX+XX=MXCn#nSample OutputnnCorrect ambiguousnCorrect impossiblenIncorrect valid

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

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭