Preface Numbering
来源:互联网 发布:淘宝psp3000 编辑:程序博客网 时间:2024/05/21 02:33
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:
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
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 7V 2
题意是给你一个数字n,把<=n的数都用罗马数字表示统计7个罗马字符出现的次数。一开始觉得要先把数字转换成罗马字符然后统计,所以拖了很久。其实可以不用转换,直接枚举就可以。
/*
ID:hanzhic1
PROG:preface
LANG:C++
*/
#include<iostream>
#include<fstream>
#include<cstring>
#include<algorithm>
using namespace std;
int ans[10];
char ch[]={'I','V','X','L','C','D','M'};
void getans(int num)
{
if(num<=0)
return;
if(num>=1000)
{
ans[7]++;
getans(num-1000);
return;
}
else if(num>=900)
{
ans[5]++;
ans[7]++;
getans(num-900);
return;
}
else if(num>=500)
{
ans[6]++;
getans(num-500);
return;
}
else if(num>=400)
{
ans[5]++;
ans[6]++;
getans(num-400);
return;
}
else if(num>=100)
{
ans[5]++;
getans(num-100);
return;
}
else if(num>=90)
{
ans[3]++;
ans[5]++;
getans(num-90);
return;
}
else if(num>=50)
{
ans[4]++;
getans(num-50);
return;
}
else if(num>=40)
{
ans[3]++;
ans[4]++;
getans(num-40);
return;
}
else if(num>=10)
{
ans[3]++;
getans(num-10);
return;
}
else if(num>=9)
{
ans[1]++;
ans[3]++;
getans(num-9);
return;
}
else if(num>=5)
{
ans[2]++;
getans(num-5);
return;
}
else if(num>=4)
{
ans[1]++;
ans[2]++;
getans(num-4);
return;
}
else if(num>=1)
{
ans[1]++;
getans(num-1);
return;
}
}
int main()
{
ofstream fout("preface.out");
ifstream fin("preface.in");
int n;
int i;
fin>>n;
memset(ans,0,sizeof(ans));
for(i = 1;i<=n;i++)
{
getans(i);
}
for(i = 1;i<8;i++)
{
if(ans[i]>0)
{
fout<<ch[i-1]<<" "<<ans[i]<<endl;
}
}
return 0;
}
Compiling...Compile: OKExecuting... Test 1: TEST OK [0.000 secs, 3176 KB] Test 2: TEST OK [0.000 secs, 3176 KB] Test 3: TEST OK [0.000 secs, 3176 KB] Test 4: TEST OK [0.000 secs, 3176 KB] Test 5: TEST OK [0.000 secs, 3176 KB] Test 6: TEST OK [0.000 secs, 3176 KB] Test 7: TEST OK [0.000 secs, 3176 KB] Test 8: TEST OK [0.000 secs, 3176 KB]All tests OK.YOUR PROGRAM ('preface') WORKED FIRST TIME! That's fantastic-- and a rare thing. Please accept these special automatedcongratulations.
Here are the test data inputs:
------- test 1 ----1------- test 2 ----20------- test 3 ----100------- test 4 ----500------- test 5 ----1000------- test 6 ----2974------- test 7 ----3213------- test 8 ----3499Keep up the good work!
- Preface Numbering
- Preface Numbering
- Preface Numbering
- Preface Numbering
- preface numbering
- USACO 2.2 Preface Numbering (preface)
- USACO2.2.1 Preface Numbering (preface)
- USACO2.2.1 Preface Numbering (preface)
- 【搜索】【USACO】Preface Numbering
- Section 2.2 Preface Numbering
- SECTION 2.2 Preface Numbering
- 2.2Preface Numbering
- USACO Preface Numbering
- usaco2.21Preface Numbering
- USACO:Preface Numbering
- USACO-Preface Numbering
- 2.2.1---Preface Numbering
- USACO 2.2 Preface Numbering
- 基于摄像头实现手写输入字符功能(视频手写)
- 云创存储第一天
- 银行业务调度系统
- Cookie Session Cache
- Android PNG渐变背景图片失真问题
- Preface Numbering
- [全程建模]类图与时序图作用的对比讨论
- 模拟滚动条
- 惹恼程序员的十件事
- 4479. Gap
- 【翻译】如何做一个项目经理?
- Android面面观——Android事件处理下(按键、触摸屏和滚动球)
- linux:rpm和yum
- 用C语言实现哈夫曼树