欧拉计划 发表于 2016-8-10 18:11:57

题目89:将罗马数字表达为最简式

Roman numerals

For a number written in Roman numerals to be considered valid there are basic rules which must be followed. Even though the rules allow some numbers to be expressed in more than one way there is always a "best" way of writing a particular number.

For example, it would appear that there are at least six ways of writing the number sixteen:



However, according to the rules only XIIIIII and XVI are valid, and the last example is considered to be the most efficient, as it uses the least number of numerals.

The 11K text file, roman.txt (right click and 'Save Link/Target As...'), contains one thousand numbers written in valid, but not necessarily minimal, Roman numerals; see About... Roman Numerals for the definitive rules for this problem.

Find the number of characters saved by writing each of these in their minimal form.

Note: You can assume that all the Roman numerals in the file contain no more than four consecutive identical units.

题目:

罗马数字的书写规则允许一个数字被用多种方式表达(具体见 Roman Numerals)。但是,对于每个数字都存在一种“最佳”书写方式。

例如,以下是数字 16 的所有合法书写形式。



最后一种写法是最高效的,因为它使用了最少的数字。

包含了一千个罗马数字,但不一定是最简形式;也就是说它们都遵循单位递减规则和相减对规则(具体见FAQ)。

找出将所有数字写成最简式一共节省的字符数。

注意:你可以认为文件中所有的每个罗马数字中同一单位的字符最多连续出现4次。


jerryxjr1220 发表于 2016-10-18 09:57:38

743


X=((b'IIII',2),(b'XXXX',2),(b'CCCC',2),(b'VIIII',1),(b'LXXXX',1),(b'DCCCC',1))
s = 'MMMMDCLXXII\r\nMMDCCCLXXXIII\r\nMMMDLXVIIII\r\nMMMMDXCV\r\nDCCCLXXII\r\nMMCCCVI\r\nMMMCDLXXXVII\r\nMMMMCCXXI\r\nMMMCCXX\r\nMMMMDCCCLXXIII\r\nMMMCCXXXVII\r\nMMCCCLXXXXIX\r\nMDCCCXXIIII\r\nMMCXCVI\r\nCCXCVIII\r\nMMMCCCXXXII\r\nMDCCXXX\r\nMMMDCCCL\r\nMMMMCCLXXXVI\r\nMMDCCCXCVI\r\nMMMDCII\r\nMMMCCXII\r\nMMMMDCCCCI\r\nMMDCCCXCII\r\nMDCXX\r\nCMLXXXVII\r\nMMMXXI\r\nMMMMCCCXIV\r\nMLXXII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MMMCCLXIV\r\nMXXII\r\nMMMCXIX\r\nMDCXXXVII\r\nMMDCCVI\r\nMCLXXXXVIII\r\nMMMCXVI\r\nMCCCLX\r\nMMMCDX\r\nCCLXVIIII\r\nMMMCCLX\r\nMCXXVIII\r\nLXXXII\r\nMCCCCLXXXI\r\nMMMI\r\nMMMCCCLXIV\r\nMMMCCCXXVIIII\r\nCXXXVIII\r\nMMCCCXX\r\nMMMCCXXVIIII\r\nMCCLXVI\r\nMMMCCCCXXXXVI\r\nMMDCCXCIX\r\nMCMLXXI\r\nMMCCLXVIII\r\nCDLXXXXIII\r\nMMMMDCCXXII\r\nMMMMDCCLXXXVII\r\nMMMDCCLIV\r\nMMCCLXIII\r\nMDXXXVII\r\nDCCXXXIIII\r\nMCII\r\nMMMDCCCLXXI\r\nMMMLXXIII\r\nMDCCCLIII\r\nMMXXXVIII\r\nMDCCXVIIII\r\nMDCCCCXXXVII\r\nMMCCCXVI\r\nMCMXXII\r\nMMMCCCLVIII\r\nMMMMDCCCXX\r\nMCXXIII\r\nMMMDLXI\r\nMMMMDXXII\r\nMDCCCX\r\nMMDXCVIIII\r\nMMMDCCCCVIII\r\nMMMMDCCCCXXXXVI\r\nMMDCCCXXXV\r\nMMCXCIV\r\nMCMLXXXXIII\r\nMMMCCCLXXVI\r\nMMMMDCLXXXV\r\nCMLXIX\r\nDCXCII\r\nMMXXVIII\r\nMMMMCCCXXX\r\nXXXXVIIII'
print(sum())

debuggerzh 发表于 2021-7-19 20:01:37

743
by cpp
/*
* @Author: your name
* @Date: 2021-07-19 17:21:13
* @LastEditTime: 2021-07-19 20:01:09
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \Nutstore\pe89.cc
*/
#include<iostream>
#include<string>
#include<map>
using namespace std;

map<char, int> rule;

int roma2arab(string s){
    int res = 0;
    for (int i = 0; i < s.size(); i++){
      switch (s)
      {
      case 'M':;
      case 'D':;
      case 'L':;
      case 'V':   
            res += rule];
            break;
            
      case 'I':
            if (i+1 < s.size() && s == 'V' || s == 'X')   res -= rule];
            else res += rule];
            break;
      case 'X':
            if (i+1 < s.size() && s == 'L' || s == 'C')   res -= rule];
            else res += rule];
            break;
      case 'C':
            if (i+1 < s.size() && s == 'D' || s == 'M')   res -= rule];
            else res += rule];
            break;
      default:
            break;
      }
    }
    return res;
}

void init_map(){
    rule['M'] = 1000;
    rule['D'] = 500;
    rule['C'] = 100;
    rule['L'] = 50;
    rule['X'] = 10;
    rule['V'] = 5;
    rule['I'] = 1;
}

void solve(int & res, int digit){   //计算后三位需要的罗马数字位数
    if (digit >= 1 && digit <= 3)   res += digit;
    else if (digit == 4 || digit == 9)    res += 2;
    else if (digit >= 5 && digit <= 8)res += digit - 5 + 1;
    return;
}

int optimize(int n){    //求最短长度
    int res = 0;
   
    int sen = n / 1000; //千位
    res += sen;

    int hyaku = n % 1000 / 100; //百位
    int juu = n % 100 / 10; //十位
    int ichi = n % 10;//各位
    solve(res, hyaku);
    solve(res, juu);
    solve(res, ichi);
   
    return res;
}

int main(int argc, char const *argv[])
{
    init_map();

    freopen("D:\\Users\\zihao\\Documents\\Nutstore\\i.in", "r", stdin);
    string roma;
    int kount = 0;
    while(getline(cin, roma)){
      int arab = roma2arab(roma);
      int shortest = optimize(arab);
      kount += roma.size() - shortest;
    }
    cout << kount << endl;
    return 0;
}

debuggerzh 发表于 2021-7-19 20:02:20

debuggerzh 发表于 2021-7-19 20:01
743
by cpp

主要得读懂罗马数字的写法
页: [1]
查看完整版本: 题目89:将罗马数字表达为最简式