UVa 344 Roman Digititis (罗马数字统计)

本文探讨了如何使用罗马数字表示书籍页码,并计算所需的不同字符数量。通过编程实现,对于指定页数范围内的罗马数字表示,确定所需不同类型的罗马数字字符总数。

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

344 - Roman Digititis

Time limit: 3.000 seconds 

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=280

Many persons are familiar with the Roman numerals for relatively small numbers. The symbols ``i", ``v", ``x", ``l", and ``c" represent the decimal values 1, 5, 10, 50, and 100 respectively. To represent other values, these symbols, and multiples where necessary, are concatenated, with the smaller-valued symbols written further to the right. For example, the number 3 is represented as ``iii", and the value 73 is represented as ``lxxiii". The exceptions to this rule occur for numbers having units values of 4 or 9, and for tens values of 40 or 90. For these cases, the Roman numeral representations are ``iv" (4), ``ix" (9), ``xl" (40), and ``xc" (90). So the Roman numeral representations for 24, 39, 44, 49, and 94 are ``xxiv", ``xxxix", ``xliv", ``xlix", and ``xciv", respectively.

The preface of many books has pages numbered with Roman numerals, starting with ``i" for the first page of the preface, and continuing in sequence. Assume books with pages having 100 or fewer pages of preface. How many ``i", ``v", ``x", ``l", and ``c" characters are required to number the pages in the preface? For example, in a five page preface we'll use the Roman numerals ``i", ``ii", ``iii", ``iv", and ``v", meaning we need 7 ``i" characters and 2 ``v" characters.

Input

The input will consist of a sequence of integers in the range 1 to 100, terminated by a zero. For each such integer, except the final zero, determine the number of different types of characters needed to number the prefix pages with Roman numerals.

Output

For each integer in the input, write one line containing the input integer and the number of characters of each type required. The examples shown below illustrate an acceptable format.

Sample Input

1
2
20
99
0

Sample Output

1: 1 i, 0 v, 0 x, 0 l, 0 c
2: 3 i, 0 v, 0 x, 0 l, 0 c
20: 28 i, 10 v, 14 x, 0 l, 0 c
99: 140 i, 50 v, 150 x, 50 l, 10 c

想了很多方案,最终觉得用switch对9个个位数和9个十位数处理最方便。


完整代码:

/*0.016s*/

#include<bits/stdc++.h>
using namespace std;

int i[105], v[105], x[105], l[105], c[105];

int main()
{
	int n, t;
	for (t = 1; t <= 100; ++t)
	{
		switch (t / 10)
		{
			case 1:
				++x[t];
				break;
			case 2:
				x[t] += 2;
				break;
			case 3:
				x[t] += 3;
				break;
			case 4:
				++x[t], ++l[t];
				break;
			case 5:
				++l[t];
				break;
			case 6:
				++x[t], ++l[t];
				break;
			case 7:
				x[t] += 2, ++l[t];
				break;
			case 8:
				x[t] += 3, ++l[t];
				break;
			case 9:
				++x[t], ++c[t];
		}
		switch (t % 10)
		{
			case 1:
				++i[t];
				break;
			case 2:
				i[t] += 2;
				break;
			case 3:
				i[t] += 3;
				break;
			case 4:
				++i[t], ++v[t];
				break;
			case 5:
				++v[t];
				break;
			case 6:
				++i[t], ++v[t];
				break;
			case 7:
				i[t] += 2, ++v[t];
				break;
			case 8:
				i[t] += 3, ++v[t];
				break;
			case 9:
				++x[t], ++i[t];
		}
		if (t == 100) ++c[t];
		i[t] += i[t - 1], v[t] += v[t - 1], x[t] += x[t - 1], l[t] += l[t - 1], c[t] += c[t - 1];
	}
	while (scanf("%d", &n), n)
		printf("%d: %d i, %d v, %d x, %d l, %d c\n", n, i[n], v[n], x[n], l[n], c[n]);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值