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:

IIIIIIIIIIIIIIII
VIIIIIIIIIII
VVIIIIII
XIIIIII
VVVI
XVI

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.


罗马数字

要正确地用罗马数字表达一个数,必须遵循一些基本规则。尽管符合规则的写法有时会多于一种,但对每个数来说总是存在一种“最好的”写法。

例如,数16就至少有六种写法:

IIIIIIIIIIIIIIII
VIIIIIIIIIII
VVIIIIII
XIIIIII
VVVI
XVI

然而,根据规则,只有XIIIIII和XVI是合理的写法,而后一种因为使用了最少的数字而被认为是最有效的写法。

在这个11K的文本文件roman.txt (右击并选择“目标另存为……”)中包含了一千个合理的罗马数字写法,但并不都是最有效的写法;有关罗马数字的明确规则,可以参考关于罗马数字

求出将这些数都写成最有效的写法所节省的字符数。

注意:你可以假定文件中的所有罗马数字写法都不包含连续超过四个相同字符。

解题

规则:

VIIII-> IX

IIII -> IV

LXXXX -> XC

XXXX -> XL

DCCCC-> CM

CCCC -> CD

上面替代的规则表示不理解

JAVA

package Level3;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.TreeSet; public class PE089{
static void run() throws IOException{
ArrayList<String> roman = getRoman();
int size = roman.size();
int res = 0;
for(int i =0;i<size ;i++){
String str = roman.get(i);
res += str.length();
str = str.replace("IIII", "IV");
str = str.replace("XXXX", "XL");
str = str.replace("CCCC", "CD");
str = str.replace("VIV", "IX");
str = str.replace("LXL", "XC");
str = str.replace("DCD", "CM");
res -= str.length();
}
System.out.println(res);
}
// 743
// running time=0s22ms
static ArrayList<String> getRoman() throws IOException{
ArrayList<String> roman = new ArrayList<String>();
String filename = "src/Level3/p089_roman.txt";
BufferedReader data = new BufferedReader(new FileReader(filename));
String line ="";
while((line = data.readLine())!= null){
roman.add(line);
// System.out.println(line);
}
return roman;
}
public static void main(String[] args) throws IOException{
long t0 = System.currentTimeMillis();
run();
long t1 = System.currentTimeMillis();
long t = t1 - t0;
System.out.println("running time="+t/1000+"s"+t%1000+"ms"); }
}

Python

# coding=gbk

import time as time
import re
def run():
filename = 'E:/java/projecteuler/src/Level3/p089_roman.txt'
file = open(filename)
ans = 0
for line in file:
a = len(line)
line = re.sub('IIII','IV',line)
line = re.sub('XXXX','XL',line)
line = re.sub('CCCC','CD',line)
line = re.sub('VIV','IX',line)
line = re.sub('LXL','XC',line)
line = re.sub('DCD','CM',line)
b = len(line)
ans = ans + a -b
print ans
#
# running time= 0.0169999599457 s
t0 = time.time()
run()
t1 = time.time()
print "running time=",(t1-t0),"s"

Project Euler 89:Roman numerals 罗马数字的更多相关文章

  1. Project Euler:Problem 89 Roman numerals

    For a number written in Roman numerals to be considered valid there are basic rules which must be fo ...

  2. Roman numerals

    Roman numerals 罗马数字的题目, 注意几个关键的数字即可: (100, 400, 500, 900) -> ('C', 'CD', 'D', 'CM'); (10, 40, 50, ...

  3. Python练习题 039:Project Euler 011:网格中4个数字的最大乘积

    本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...

  4. Python练习题 030:Project Euler 002:偶数斐波那契数之和

    本题来自 Project Euler 第2题:https://projecteuler.net/problem=2 # Each new term in the Fibonacci sequence ...

  5. Roman Numerals All In One

    Roman Numerals All In One 罗马数字 refs https://www.mathsisfun.com/roman-numerals.html https://www.maths ...

  6. [project euler] program 4

    上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...

  7. Python练习题 029:Project Euler 001:3和5的倍数

    开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...

  8. Project Euler 9

    题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...

  9. Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.

    In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...

随机推荐

  1. Windows7系统禁用USB和启用USB方法

    被迫装了XX软件之后,无线网络和USB都被禁用了,XX软件还不能被卸载.只能用PE进去时候把XX软件安装目录进行删除,但是删除之后还是不能识别U盘,从网上找到办法一看是注册表的项被修改了. 注册表项为 ...

  2. js IDE WebStorm 注册码

    webStorm : UserName:William ===== LICENSE BEGIN ===== 45550-12042010 00001SzFN0n1bPII7FnAxnt0DDOPJA  ...

  3. HotSpot Builder Utility安装指南

    系统需求硬件- 一台带有1个以太网卡的电脑(宿主机)- 一个无线路由器 软件- VirtualBox 4.1或更高的版本.下载网址:http://www.virtualbox.org/- 我们提供的最 ...

  4. android LayoutInflater.inflate()的参数介绍

    LayoutInflater.inflate()的作用就是将一个xml定义的布局文件实例化为view控件对象: 与findViewById区别: LayoutInflater.inflate是加载一个 ...

  5. python之setattr,getattr,hasattr

    可以使用setattr(), getattr(), hasattr()动态对实例进行操作. 相当于Java中的反射机制, 或者更确切地, 像JavaScript中属性操作. 具体属性: __dict_ ...

  6. boost-内存管理

    Boost智能指针——scoped_ptr boost::scoped_ptr和std::auto_ptr非常类似,是一个简单的智能指针,它能够保证在离开作用域后对象被自动释放. boost::sco ...

  7. WPF TextBox 的 EventTrigger & 重写控件

    遇到一个需求,在textbox获得焦点的时候,调用一个外部的软键盘. 这可以用两个不同的方法来达到目的. 1.EventTrigger 首先定义一个Style <Style x:Key=&quo ...

  8. margin-top相对谁的问题

    根据规范,一个盒子如果没有上补白(padding-top)和上边框(border-top),那么这个盒子的上边距会和其内部文档流中的第一个子元素的上边距重叠.意思便是:如果你只想margin相对于父标 ...

  9. Team Homework #3

    我们组采访了以下几组学长学姐.因为隐私问题我们不会写出他们的个人信息. 1:平均每周所花时间:10:平均写的代码总数:2000:最有用的部分:锻炼团队合作精神:最没用的部分:写博客:改进:完全不需要博 ...

  10. [原创] zabbix学习之旅四:mail客户端安装

    相信大家使用zabbix的最主要目的就是当被监控机器发生故障时,能通过zabbix获得第一时间的报警提醒.zabbix常用的报警媒介有email,短信,jabber和脚本,这其中脚本类型最为灵活,尤其 ...