[usaco]罗马数字

Preface Numbering

A certain book's prefaces are numbered in upper case Roman numerals. Traditional Roman numeral values use a single letter to represent a certain subset of decimal numbers. Here is the standard set:

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

As many as three of the same marks that represent 10n may be placed consecutively to form other numbers:

III is 3
CCC is 300
Marks that have the value 5x10n are never used consecutively.

Generally (with the exception of the next rule), marks are connected together and written in descending order to form even more numbers:

CCLXVIII = 100+100+50+10+5+1+1+1 = 268
Sometimes, a mark that represents 10^n is placed before a mark of one of the two next higher values (I before V or X; X before L or C; etc.). In this case, the value of the smaller mark is SUBTRACTED from the mark it precedes:

IV = 4
IX = 9
XL = 40
This compound mark forms a unit and may not be combined to make another compound mark (e.g., IXL is wrong for 39; XXXIX is correct).

Compound marks like XD, IC, and XM are not legal, since the smaller mark is too much smaller than the larger one. For XD (wrong for 490), one would use CDXC; for IC (wrong for 99), one would use XCIX; for XM (wrong for 990), one would use CMXC. 90 is expressed
XC and not LXL, since L followed by X connotes that successive marks are X or smaller (probably, anyway).

Given N (1 <= N < 3,500), the number of pages in the preface of a book, calculate and print the number of I's, V's, etc. (in order from lowest to highest) required to typeset all the page numbers (in Roman numerals) from 1 through N. Do not print letters
that do not appear in the page numbers specified.

If N = 5, then the page numbers are: I, II, III, IV, V. The total number of I's is 7 and the total number of V's is 2.

PROGRAM NAME: preface
INPUT FORMAT
A single line containing the integer N.
SAMPLE INPUT (file preface.in)
5

OUTPUT FORMAT
The output lines specify, in ascending order of Roman numeral letters, the letter, a single space, and the number of times that letter appears on preface page numbers. Stop printing letter totals after printing the highest value letter used to form preface
numbers in the specified set.
SAMPLE OUTPUT (file preface.out)
I 7
V 2

 

------------------------------------------------------------------------------------------------------------------------
题目给出一个整数N,要求求出1-》N的所有的罗马数表示法,然后统计各个字母的数量。

由罗马数的表示法,可以看出,每一位上的表示是基本相同的,
比如1->9
I II III IV V VI VII VIII IX
10 ->90
X XX XXX XL L LX LXX LXXX XC

从上边可以看出一些规律。
因此每一位数字需要3个字母即可表示出来。
我们定义以下二维数组:
char index1[4][3]={
 {'I','V','X'},
 {'X','L','C'},
 {'C','D','M'},
 {'M'}
};
因此可以用这个数组表示一位数:
10^r->9*10^r:
index1[r][0]   index1[r][0]index1[r][0]   index1[r][0]index1[r][0]index1[r][0] index1[r][0]index1[r][1] index1[r][1]
index1[r][1]index1[r][0] index1[r][1]index1[r][0]index1[r][1] index1[r][1]index1[r][0]index1[r][1]index1[r][0]index1[r][0]index1[r][0] index1[r][0]index1[r][2]
因此我的解法是:
---------------------------------------------------------------------------------------------------------------------------
 

/*
ID: yunleis2
PROG: preface
LANG: C++
*/
#include<fstream>
#include<iostream>
using namespace std;
int result[7]={0};
char str[7]={'I','V',
'X','L',
'C','D','M'};
void getNum1(int r,int c);
int getindex11(char c);
char index1[4][3]={
	{'I','V','X'},
	{'X','L','C'},
	{'C','D','M'},
	{'M'}
};
int main()
{
	fstream fin("preface.in",ios::in);
	int N;
	fin>>N;
	for(int i=1;i<=N;i++)
	{
		int r=0;
		int t=i;
		while(t!=0)
		{
			getNum1(r,t%10);
			r++;
			t=t/10;
		}
	}
	fstream fout("preface.out",ios::out);
	for(int i=0;i<7;i++)
	{
		if(result[i]!=0)
		{
			fout<<str[i]<<" "<<result[i]<<endl;
		}
	}
}
void getNum1(int r,int c)
{

	if(c==1)
	{
		result[getindex11(index1[r][0])]++;
	}
	if(c==2)
	{
		result[getindex11(index1[r][0])]+=2;
	}

	if(c==3)
	{
		result[getindex11(index1[r][0])]+=3;
	}
	if(c==4)
	{
		result[getindex11(index1[r][0])]++;
		result[getindex11(index1[r][1])]++;
	}
	if(c==5)
	{
		result[getindex11(index1[r][1])]++;
	}
	if(c==6)
	{
		result[getindex11(index1[r][1])]++;
		result[getindex11(index1[r][0])]++;
	}
	if(c==7)
	{
		result[getindex11(index1[r][1])]++;
		result[getindex11(index1[r][0])]++;
		result[getindex11(index1[r][0])]++;
	}
	if(c==8)
	{
		result[getindex11(index1[r][1])]++;
		result[getindex11(index1[r][0])]++;
		result[getindex11(index1[r][0])]++;
		result[getindex11(index1[r][0])]++;
	}
	if(c==9)
	{
		result[getindex11(index1[r][0])]++;
		result[getindex11(index1[r][2])]++;
	}
}
int getindex11(char c)
{
	if(c=='I')
		return 0;
	if(c=='V')
		return 1;
	if(c=='X')
		return 2;
	if(c=='L')
		return 3;
	if(c=='C')
		return 4;
	if(c=='D')
		return 5;
	if(c=='M')
		return 6;
}

 

时间: 2024-10-02 20:48:16

[usaco]罗马数字的相关文章

Word 2007 输入省略号和罗马数字

  中文输入法状态下也可输入省略号 许多人都误以为:在Word中,只有切换到英文输入法状态下才可以输入省略号,否则敲击键盘上的"."键,就只能输入中文标点符号中的句号而非英文的省略号了.其实不然,在中文输入法状态下只要同时按下"Ctrl+Alt+."键也可以快速输入省略号. Word中另法输入罗马数字 在Word中,用"插入"→"符号"的方法可以输入不大于XII的罗马数字.还有一种方法则是:多次同时按下"Ctrl+A

MathType公式编辑器中如何输入罗马数字

  具体操作过程如下: 1.根据自己的使用习惯将MathType软件打开,进入到编辑窗口. 2.在编辑窗口中,在MathType菜单中选择"编辑"--"插入符号"命令,刚会弹出来一个插入符号窗口. 点击"编辑"--"插入符号" 3.在"插入符号"窗口中,将"查看"设置为"样式"."文本".在下面的符号模板中找到罗马字母进行输入.罗马数字1就是大写的

lucene无法搜索到含有罗马数字的字符串

问题描述 lucene无法搜索到含有罗马数字的字符串 建立索引的时候未分词,通过lukeall可以看到索引内容如下: 搜索的时候搜 三江Ⅰ回线* 没有结果 其他包含阿拉伯数字和中文数字的都正常 有人知道什么情况么 解决方案 看看你的I是不是中文全角的,还是英文的i的大写 解决方案二: 显然是分词没有处理好,自己在词库实现自己的特定分词需求就可以搞定

usaco的一道题:packrec是暴搜求第六种方案思路

问题描述 usaco的一道题:packrec是暴搜求第六种方案思路 usaco上有一道我觉得对于我来说很变态的暴搜题,翻译过来是这样的 给定4个矩形块找出一个最小的封闭矩形将这4个矩形块放入但不得相互重叠.所谓最小矩形指该矩形面积最小. 所有4个矩形块的边都与封闭矩形的边相平行图1示出了铺放4个矩形块的6种方案.这6种方案仅只是可能的基本铺放方案.因为其它方案能由基本方案通过旋转和镜像反射得到. 可能存在满足条件且有着同样面积的各种不同的封闭矩形你应该输出所有这些封闭矩形的边长. INPUT F

c-C 在做USACO Greedy Gift Givers的时候遇到bus error 10

问题描述 C 在做USACO Greedy Gift Givers的时候遇到bus error 10 下面是我的代码 (C语言) #include <stdio.h> #include <stdlib.h> #include <string.h> FILE *input_file, *output_file; int total_people, giving_to_counter, accepting, having[9]; char giver[9][13], rec

[usaco]超级素数 superprime

偶也,纪念一次成功,首先发测试结果 USER: Ma yunlei [yunleis2]TASK: sprimeLANG: C++ Compiling...Compile: OK Executing...   Test 1: TEST OK [0.000 secs, 3028 KB]   Test 2: TEST OK [0.000 secs, 3028 KB]   Test 3: TEST OK [0.000 secs, 3028 KB]   Test 4: TEST OK [0.000 se

usaco 2005 奶牛大学 求c++程序

问题描述 usaco 2005 奶牛大学 求c++程序 问题描述:Bessie统计到人类拥有很多大学可以去就读,而奶牛们却一个大学也没有.为了解决这个问题,她和她的同伴们建立了一所奶牛大学Moo大学.为了防止笨牛入学,学校的创立者搞了一个奶牛智力测试(CSAT),它的分数S在区间1.. 2 109内.Moo大学的学费很昂贵:不是所有奶牛都能负担.事实上,大多数奶牛需要一些财政帮助aid(0 aid 105).政府不会给奶牛任何资金,所以所有的资金都来自于学校有限的资金F.更糟的是,虽然有c头奶牛

usaco java-usaco提交问题,是因为java的版本问题吗

问题描述 usaco提交问题,是因为java的版本问题吗 我直接把usaco里面的例子test.java复制粘贴然后改了下ID提交了,结果它依然报错了,这是什么情况啊 解决方案 跪求大神回答啊!!!

[usaco] 5.4.4 Betsy&amp;#39;s Tour

 好久没作usaco了.过了个年,人都懒了. Betsy's Tour Don Piele A square township has been divided up into N2 square plots (1 <= N <= 7). The Farm is located in the upper left plot and the Market is located in the lower left plot. Betsy takes her tour of the township