Code Monkey home page Code Monkey logo

Comments (6)

airwin avatar airwin commented on May 30, 2024 1

穷举。。

function resolve(num) {
  var nMap = [
    ['X', 'V', 'I'], // 个位
    ['C', 'L', 'X'], // 十位
    ['M', 'D', 'C'], // 百位
    ['一万', '五千', 'M'], // 千位
    ['十万', '五万', '一万'], // 万位 ...
  ]

  return (num + '').split('').reverse().map(function(m, i){
    switch (parseInt(m)) {
      case 0:
        return ''
      case 1:
      case 2:
      case 3:
        return nMap[i][2].repeat(m)
      case 4:
        return nMap[i][2] + nMap[i][1]
      case 5:
        return nMap[i][1]
      case 6:
      case 7:
      case 8:
        return nMap[i][1] + nMap[i][2].repeat(m - 5)
      case 9:
        return nMap[i][2] + nMap[i][0]
    }
  }).reverse().join('')
}

console.assert(resolve(1437) === 'MCDXXXVII', resolve(1437))
console.log(6666, '->', resolve(6666))

from daily-algorithms.

barretlee avatar barretlee commented on May 30, 2024

再给出一张参考表

罗马数字

from daily-algorithms.

barretlee avatar barretlee commented on May 30, 2024

枚举应该是最简单的...

function resolve(n) {
  var map = [
    ['', "M", "MM", "MMM"],
    ['', "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"],
    ['', "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"],
    ['', "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]
  ];
  return map[0][~~(n / 1000)] + 
         map[1][~~(n % 1000 / 100)] + 
         map[2][~~(n % 100 / 10)] + 
         map[3][n % 10];
}

console.assert(resolve(1437) === 'MCDXXXVII', resolve(1437));

from daily-algorithms.

barretlee avatar barretlee commented on May 30, 2024

增加题目难度(★★★):如果罗马数字能够超过 3999,比如有其他符号可以替代更高位,给出通用解。

from daily-algorithms.

zswang avatar zswang commented on May 30, 2024

混个眼熟

let map = [
  { char: 'I', value: 1 },
  { char: 'IV', value: 4 },
  { char: 'V', value: 5 },
  { char: 'IX', value: 9 },
  { char: 'X', value: 10 },
  { char: 'XL', value: 40 },
  { char: 'L', value: 50 },
  { char: 'XC', value: 90 },
  { char: 'C', value: 100 },
  { char: 'CD', value: 400 },
  { char: 'D', value: 500 },
  { char: 'CM', value: 900 },
  { char: 'M', value: 1000 },
  { char: 'Mↁ', value: 4000 },
  { char: 'ↁ', value: 5000 },
  { char: 'Mↂ', value: 9000 },
  { char: 'ↂ', value: 10000 },
]

function resolve(num) {
  let result = ''
  let i = map.length - 1
  while (i >= 0) {
    let item = map[i]
    if (item.value <= num) {
      result += item.char
      num -= item.value
    } else {
      i--
    }
  }
  return result
}

from daily-algorithms.

szu-bee avatar szu-bee commented on May 30, 2024
function convert(num) {
  const roman = {
    'M': 1000,
    'CM': 900,
    'D': 500,
    'CD': 400,
    'C': 100,
    'XC': 90,
    'L': 50,
    'XL': 40,
    'X': 10,
    'IX': 9,
    'V': 5,
    'IV': 4,
    'I': 1
  };
  
  let result = '';
  for (let key of Object.keys(roman)) {
    const freq = Math.floor(num / roman[key]);
    num -= freq * roman[key];
    result += key.repeat(freq);
  }

  return result;
}

from daily-algorithms.

Related Issues (16)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.