USACO 2.2 Preface Numbering

本文介绍了一种算法,用于统计从1到给定整数n范围内所有数字转换为罗马数字后的字符出现次数。通过分析罗马数字的构成规律,实现了高效计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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这些数转化为罗马数字表达方式后,每个字母的使用量。 

思路:找到罗马计数的规律,如下图,可以发现个、十、百、千位上计数法则都是一样的,只是表示的字母不同,且每一位上的数字表示只需用到三种字母,如个位需要I V X。所以我们在统计的时候可以按位进行,每一位上根据数字的值可以确定它使用的三种字母的数目。 代码中 num[10] ={0,1,2,3,1,0,1,2,3,1} 存储的是某一位数字0~9表示成罗马数字所需要的最小计数单位的个数(如个位来说,所需要的I的个数)。



完整代码如下:

/*
ID: gjj50201
LANG: C++
TASK: preface	
*/

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;

string roma = "IVXLCDM";
int num[10]={0,1,2,3,1,0,1,2,3,1};
int ans[7];

void solve(int n){
	int t, i = 0;
	while(n){
		t = n%10;
		n /= 10;
		ans[i] += num[t];
		if(t == 9)
			ans[i+2] ++;
		else if(t >= 4)
			ans[i+1] ++;
		i += 2;
	}
}

int main(){
	freopen("preface.in","r",stdin);
	freopen("preface.out","w",stdout);
	int n;
	cin>>n;
	for(int i=1;i<=n;i++)
		solve(i);
	for(int i=0;i<7;i++)
		if(ans[i]>0)
			cout<<roma[i]<<" "<<ans[i]<<endl;
	return 0;
}


基于Spring Boot搭建的一个多功能在线学习系统的实现细节。系统分为管理员和用户两个主要模块。管理员负责视频、文件和文章资料的管理以及系统运营维护;用户则可以进行视频播放、资料下载、参与学习论坛并享受个性化学习服务。文中重点探讨了文件下载的安全性和性能优化(如使用Resource对象避免内存溢出),积分排行榜的高效实现(采用Redis Sorted Set结构),敏感词过滤机制(利用DFA算法构建内存过滤树)以及视频播放的浏览器兼容性解决方案(通过FFmpeg调整MOOV原子位置)。此外,还提到了权限管理方面自定义动态加载器的应用,提高了系统的灵活性和易用性。 适合人群:对Spring Boot有一定了解,希望深入理解其实际应用的技术人员,尤其是从事在线教育平台开发的相关从业者。 使用场景及目标:适用于需要快速搭建稳定高效的在线学习平台的企业或团队。目标在于提供一套完整的解决方案,涵盖从资源管理到用户体验优化等多个方面,帮助开发者更好地理解和掌握Spring Boot框架的实际运用技巧。 其他说明:文中不仅提供了具体的代码示例和技术思路,还分享了许多实践经验教训,对于提高项目质量有着重要的指导意义。同时强调了安全性、性能优化等方面的重要性,确保系统能够应对大规模用户的并发访问需求。
标题基于SpringBoot的学生学习成果管理平台研究AI更换标题第1章引言介绍研究背景、目的、意义以及论文结构。1.1研究背景与目的阐述学生学习成果管理的重要性及SpringBoot技术的优势。1.2研究意义分析该平台对学生、教师及教育机构的意义。1.3论文方法与结构简要介绍论文的研究方法和整体结构。第2章相关理论与技术概述SpringBoot框架、学习成果管理理论及相关技术。2.1SpringBoot框架简介介绍SpringBoot的基本概念、特点及应用领域。2.2学习成果管理理论基础阐述学习成果管理的核心理论和发展趋势。2.3相关技术分析分析平台开发所涉及的关键技术,如数据库、前端技术等。第3章平台需求分析与设计详细分析平台需求,并设计整体架构及功能模块。3.1需求分析从学生、教师、管理员等角度对平台需求进行深入分析。3.2整体架构设计设计平台的整体架构,包括技术架构和逻辑架构。3.3功能模块设计具体设计平台的核心功能模块,如成果展示、数据分析等。第4章平台实现与测试阐述平台的实现过程,并进行功能测试与性能分析。4.1平台实现详细介绍平台的开发环境、关键代码实现及技术难点解决方案。4.2功能测试对平台各项功能进行全面测试,确保功能正确无误。4.3性能分析分析平台的性能指标,如响应时间、并发处理能力等。第5章平台应用与效果评估探讨平台在实际教学中的应用,并对其效果进行评估。5.1平台应用案例选取典型应用案例,展示平台在实际教学中的使用情况。5.2效果评估方法介绍平台效果评估的具体方法和指标。5.3评估结果分析根据评估数据,对平台的应用效果进行深入分析。第6章结论与展望总结论文的主要研究成果,并指出未来研究方向。6.1研究结论概括性地阐述论文的研究结论和主要贡献。6.2研究展望针对当前研究的不足之处,提出未来改进和扩展的方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值