2016-08-29 2 views
0
function convertToRoman(num) { 

    var thisMap = { 

    1:[1], 
    2:[1, 1], 
    3:[1, 1, 1], 
    4:[1, 5], 
    5:[5], 
    6:[5, 1], 
    7:[5, 1, 1], 
    8:[5, 1, 1, 1], 
    9:[1, 10], 
    0:[0] 

    }; 

    var numMap = { 

    1000:"M", 
    500:"D", 
    100:"C", 
    50:"L", 
    10:"X", 
    5:"V", 
    1:"I" 

    }; 

    numArr = num.toString().split(""); 

    var thisIndex = 1; 

    var tallyArr = []; 

    for (var i = numArr.length - 1; i >= 0; i--) { 

    tallyArr.unshift(thisMap[numArr[i]]); 

    } 

    thisIndex = Math.pow(10, tallyArr.length - 1); 

    checkArr = []; 

    <<<BUG HERE>>> 

    for (var x = 0; x < tallyArr.length; x++) { 

    for (var y = 0; y < tallyArr[x].length; y++) { 

     tallyArr[x][y] *= thisIndex; 

    } 

    thisIndex = thisIndex/10; 

    } 

    <<</BUG HERE>>> 

    var finalArr = []; 

    for (var a = 0; a < tallyArr.length; a++) { 

    for (var b = 0; b < tallyArr[a].length; b++) { 

     finalArr.push(numMap[tallyArr[a][b]]); 

    } 

    } 

    finalAnswer = finalArr.join(""); 

    return finalAnswer; 

} 

convertToRoman(88); 

그래서 숫자를 자바 스크립트로 로마 숫자로 변환하는 기능입니다. 기본적으로 thisMap을 사용하여 모든 숫자를 올바른 형식으로 포맷 한 다음 thisIndex를 사용하여 1000, 100 또는 10으로 곱한 다음 numMap과 비교하여 올바른 로마 숫자를 얻습니다.자바에서 로마 숫자로 변환 - 이상한 버그

이러한 경우에는 44, 99, 또는 3999

를 제외하고, 테스트 케이스의 대부분에서 작동하는 것 같다

, 그래서 44 XLXL이되고, 때를 잘못 양만큼의 숫자를 곱 보인다 XLIV 여야합니다.

버그가 < < 사이에있는 것 같습니다. >> 그 이유는 숫자가 잘못 곱해진 것 같기 때문입니다.

그러나 문제는 발견 할 수 없습니다.

감사합니다.

+0

에 체크 아웃 : http://blog.stevenlevithan.com/archives/javascript-roman-numeral-converter이 SO http://stackoverflow.com/a/ 9083076/5324369 작동 알고리즘이 있습니다. –

답변

0

다음과 같이 시도해보십시오. x 루프는 마지막 길이를 제외한 의 길이를 모두 통과해야합니다.

function convertToRoman(num) { 

    // ... code ... 

    for (var x = 0; x < tallyArr.length - 1; x++) { 
    for (var y = 0; y < tallyArr[x].length; y++) { 
     tallyArr[x][y] *= thisIndex; 
    } 
    thisIndex = thisIndex/10; 
    } 

    // ... more code ... 
} 
0

귀하의 솔루션을 통해 조금 보인다 설계 때로는 단순이 더 나은 어떤 영리한 대답처럼 보일 수 있으며 지나치게 웅변 솔루션을 찾는 것은 당신을 여행 할 수 지나치게 복잡합니다.

function convertToRoman(num) { 
    var output = ""; 

    var numMap = [ 
    { limit: 1000, value: "M" }, 
    { limit: 900, value: "CM" }, 
    { limit: 500, value: "D" }, 
    { limit: 400, value: "CD" }, 
    { limit: 100, value: "C" }, 
    { limit: 90, value: "XC" }, 
    { limit: 50, value: "L" }, 
    { limit: 40, value: "XL" }, 
    { limit: 10, value: "X" }, 
    { limit: 9, value: "IX" }, 
    { limit: 5, value: "V" }, 
    { limit: 4, value: "IV" }, 
    { limit: 1, value: "I" } 
    ]; 

    for(var index = 0; index < numMap.length; index++) { 
     var value = numMap[index].value, 
      limit = numMap[index].limit; 
     while(num >= limit) { 
     output += value; 
     num -= limit; 
     } 
    } 

    return output; 
} 

alert(convertToRoman(1)); 
alert(convertToRoman(4)); 
alert(convertToRoman(5)); 
alert(convertToRoman(9)); 
alert(convertToRoman(10)); 
alert(convertToRoman(88)); 
alert(convertToRoman(2016)); 

JSFiddle

0
var romanNumber = [ 
      [1, 'I'], [2, 'II'], [3, 'III'],[4, 'IV'], 
      [5, 'V'], [6, 'VI'],[7, 'VII'], [8, 'VIII'], 
      [9, 'IX'],[10, 'X'] 
]; 


function convertToRoman(number) { 
    if (number === 0) { 
     return ''; 
    } 
    for (var i = 0; i < romanNumber.length; i++) { 
     if (number === romanNumber[i][0]) { 
     return romanNumber[i][1]; 
     } 
    } 
} 
console.log(convertToRoman(1)); 
console.log(convertToRoman(2)); 
console.log(convertToRoman(3)); 
console.log(convertToRoman(4)); 
console.log(convertToRoman(5)); 
console.log(convertToRoman(6)); 
console.log(convertToRoman(7)); 
console.log(convertToRoman(8)); 
console.log(convertToRoman(9)); 
console.log(convertToRoman(10));