注意到一点后就会觉得这道题是非常简单的了,罗马字母表示法总是一个数字一个数字的表示,比如说用罗马字母来表示数字“3456”时,先表示出“3000(MMM)”,然后是“400(CD)”,然后是“50(L)”,最后是“6(VI)”,最后把四个罗马字母链连接起来就是“3456(MMMCDLVI)”的准确表示
那么计算表示“3456”需要多少罗马字母时,就可以把“3000”、“400”、“50”、“6”这四个数字所需字母数相加即可
Code
/**//*
ID: sdjllyh1
PROG: preface
LANG: JAVA
complete date: 2008/12/9
author: LiuYongHui From GuiZhou University Of China
more article: www.cnblogs.com/sdjls
*/
import java.io.*;
import java.util.*;
public class preface
{
private static int n;
private static int i, v, x, l, c, d, m;//记录每个罗马字母出现的次数
public static void main(String[] args) throws IOException
{
init();
run();
output();
System.exit(0);
}
private static void run()
{
//分别计算表示从1到n的n个数字所需罗马字母数
for (int i = 1; i <= n; i++)
{
splitAndCalculate(i);
}
}
//把一个数字拆分为1、2、3、……、10、20、30、……、100、200、300、……、1000、……的形式后,计算表示每个部分所需要的各种罗马字母数
private static void splitAndCalculate(int value)
{
int pos = 1;
while (value > 0)
{
calculate((value % 10) * pos);
value = value / 10;
pos = pos * 10;
}
}
//计算表示形如1、2、3、……、10、20、30、……、100、200、300、……、1000、……的数字所需各种罗马字母数
private static void calculate(int value)
{
switch (value)
{
case 1:
i += 1;
break;
case 2:
i += 2;
break;
case 3:
i += 3;
break;
case 4:
i += 1; v += 1;
break;
case 5:
v += 1;
break;
case 6:
v += 1; i += 1;
break;
case 7:
v += 1; i += 2;
break;
case 8:
v += 1; i += 3;
break;
case 9:
i += 1; x += 1;
break;
case 10:
x += 1;
break;
case 20:
x += 2;
break;
case 30:
x += 3;
break;
case 40:
x += 1; l += 1;
break;
case 50:
l += 1;
break;
case 60:
l += 1; x += 1;
break;
case 70:
l += 1; x += 2;
break;
case 80:
l += 1; x += 3;
break;
case 90:
x += 1; c += 1;
break;
case 100:
c += 1;
break;
case 200:
c += 2;
break;
case 300:
c += 3;
break;
case 400:
c += 1; d += 1;
break;
case 500:
d += 1;
break;
case 600:
d += 1; c += 1;
break;
case 700:
d += 1; c += 2;
break;
case 800:
d += 1; c += 3;
break;
case 900:
c += 1; m += 1;
break;
case 1000:
m += 1;
break;
case 2000:
m += 2;
break;
case 3000:
m += 3;
break;
default:
break;
}
}
private static void init() throws IOException
{
BufferedReader f = new BufferedReader(new FileReader("preface.in"));
n = Integer.parseInt(f.readLine());
f.close();
}
private static void output() throws IOException
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("preface.out")));
if (i > 0)
{
out.println("I " + i);
}
if (v > 0)
{
out.println("V " + v);
}
if (x > 0)
{
out.println("X " + x);
}
if (l > 0)
{
out.println("L " + l);
}
if (c > 0)
{
out.println("C " + c);
}
if (d > 0)
{
out.println("D " + d);
}
if (m > 0)
{
out.println("M " + m);
}
out.close();
}
}
/**//*
ID: sdjllyh1
PROG: preface
LANG: JAVA
complete date: 2008/12/9
author: LiuYongHui From GuiZhou University Of China
more article: www.cnblogs.com/sdjls
*/
import java.io.*;
import java.util.*;
public class preface
{
private static int n;
private static int i, v, x, l, c, d, m;//记录每个罗马字母出现的次数
public static void main(String[] args) throws IOException
{
init();
run();
output();
System.exit(0);
}
private static void run()
{
//分别计算表示从1到n的n个数字所需罗马字母数
for (int i = 1; i <= n; i++)
{
splitAndCalculate(i);
}
}
//把一个数字拆分为1、2、3、……、10、20、30、……、100、200、300、……、1000、……的形式后,计算表示每个部分所需要的各种罗马字母数
private static void splitAndCalculate(int value)
{
int pos = 1;
while (value > 0)
{
calculate((value % 10) * pos);
value = value / 10;
pos = pos * 10;
}
}
//计算表示形如1、2、3、……、10、20、30、……、100、200、300、……、1000、……的数字所需各种罗马字母数
private static void calculate(int value)
{
switch (value)
{
case 1:
i += 1;
break;
case 2:
i += 2;
break;
case 3:
i += 3;
break;
case 4:
i += 1; v += 1;
break;
case 5:
v += 1;
break;
case 6:
v += 1; i += 1;
break;
case 7:
v += 1; i += 2;
break;
case 8:
v += 1; i += 3;
break;
case 9:
i += 1; x += 1;
break;
case 10:
x += 1;
break;
case 20:
x += 2;
break;
case 30:
x += 3;
break;
case 40:
x += 1; l += 1;
break;
case 50:
l += 1;
break;
case 60:
l += 1; x += 1;
break;
case 70:
l += 1; x += 2;
break;
case 80:
l += 1; x += 3;
break;
case 90:
x += 1; c += 1;
break;
case 100:
c += 1;
break;
case 200:
c += 2;
break;
case 300:
c += 3;
break;
case 400:
c += 1; d += 1;
break;
case 500:
d += 1;
break;
case 600:
d += 1; c += 1;
break;
case 700:
d += 1; c += 2;
break;
case 800:
d += 1; c += 3;
break;
case 900:
c += 1; m += 1;
break;
case 1000:
m += 1;
break;
case 2000:
m += 2;
break;
case 3000:
m += 3;
break;
default:
break;
}
}
private static void init() throws IOException
{
BufferedReader f = new BufferedReader(new FileReader("preface.in"));
n = Integer.parseInt(f.readLine());
f.close();
}
private static void output() throws IOException
{
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("preface.out")));
if (i > 0)
{
out.println("I " + i);
}
if (v > 0)
{
out.println("V " + v);
}
if (x > 0)
{
out.println("X " + x);
}
if (l > 0)
{
out.println("L " + l);
}
if (c > 0)
{
out.println("C " + c);
}
if (d > 0)
{
out.println("D " + d);
}
if (m > 0)
{
out.println("M " + m);
}
out.close();
}
}