[usaco]罗马数字

简介: <p>Preface Numbering</p> <p>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 numb

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;
}


 

目录
相关文章
|
5月前
|
数据采集 缓存 监控
唯品会 API 开发痛点解析:从权限申请到数据清洗的实战经验
本文深入解析唯品会开放平台(VOP)API开发全流程,涵盖权限申请、签名机制、数据清洗、性能优化等核心挑战与实战解决方案,助力开发者高效构建稳定可靠的电商数据整合系统。
|
关系型数据库 分布式数据库 数据库
PolarDB PostgreSQL版:Oracle兼容的高性能数据库
PolarDB PostgreSQL版是一款高性能的数据库,具有与Oracle兼容的特性。它采用了分布式架构,可以轻松处理大量的数据,同时还支持多种数据类型和函数,具有高可用性和可扩展性。它还提供了丰富的管理工具和性能优化功能,为企业提供了可靠的数据存储和处理解决方案。PolarDB PostgreSQL版在数据库领域具有很高的竞争力,可以满足各种企业的需求。
|
12月前
|
运维 数据可视化 数据挖掘
见证奇迹!J 人软件升级维护团队协作的 6 款办公软件来了!
在软件升级与维护的关键环节,高效的团队协作和个人学习效率至关重要。本文深入剖析了6款卓越的可视化团队协作办公软件:板栗看板、Trello、Asana、Jira、ClickUp和Monday.com。这些工具通过直观的任务呈现、精准的流程管理、便捷的协作沟通、多样的视图切换、灵活的自定义配置及深入的数据分析,助力J人团队提升效率和质量,推动项目顺利进行。选择合适的工具,为团队插上腾飞的翅膀,创造辉煌业绩。
226 8
|
前端开发 JavaScript API
前端技术探索:从基础到未来趋势的深度剖析
前端技术探索:从基础到未来趋势的深度剖析
316 1
|
文字识别 PyTorch Go
从零开始的OCR之旅
本文介绍了如何配置环境并使用EasyOCR库进行OCR任务,包括安装依赖、下载必要的模型包,并提供了一个简单的使用示例。
从零开始的OCR之旅
|
人工智能 安全 测试技术
EXAONE 3.5:LG 推出的开源 AI 模型,采用 RAG 和多步推理能力降低模型的幻觉问题
EXAONE 3.5 是 LG AI 研究院推出的开源 AI 模型,擅长长文本处理,能够有效降低模型幻觉问题。该模型提供 24 亿、78 亿和 320 亿参数的三个版本,支持多步推理和检索增强生成技术,适用于多种应用场景。
334 9
EXAONE 3.5:LG 推出的开源 AI 模型,采用 RAG 和多步推理能力降低模型的幻觉问题
|
负载均衡 算法 Java
分布式系列教程(10) -分布式协调工具Zookeeper(负载均衡原理实现)
分布式系列教程(10) -分布式协调工具Zookeeper(负载均衡原理实现)
282 0
|
C# 数据安全/隐私保护 开发者
『.NET』.NET 中常用的AOP框架——Castle
📣读完这篇文章里你能收获到 - AOP概念介绍 - 结合具体代码讲解.NET项目接入Castle
558 0
『.NET』.NET 中常用的AOP框架——Castle
|
网络协议 Windows
Wireshark的下载安装及简单使用教程
Wireshark的下载安装及简单使用教程
684 0
|
Web App开发 Unix 测试技术
什么是兼容性测试?
什么是兼容性测试?
620 0

热门文章

最新文章