Preface Numbering
来源:互联网 发布:数据归一化迭代公式 编辑:程序博客网 时间:2024/05/22 06:39
题意:给定页数N,求页码1~N(用罗马数字表示)中一共有多少个I、V、X、L、C、D、M
解题思路:
- 读入N
- 从1遍历至N,在遍历过程中统计各个字母的个数
- 统计中参考百度百科---罗马数字----组数规则(http://baike.baidu.com/link?url=Rt-mNHT_kqOLjmxWLE7mZFAl7iH4nmdYkWRbAzSPvM48GCiw6lRiVDUhAYV_XSng)
代码:
/*ID: zc.rene1LANG: CPROG: preface */#include<stdio.h>#include<stdlib.h>#include<string.h>enum letters{I=0, V, X, L, C, D, M};char letter_map[7] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};int result[7];void CalculateLetters(int num){ result[M] += (num/1000); num %= 1000; if (num >= 900) {result[C]++;result[M]++;num -= 900; } if (num >= 500) {result[D]++;num -= 500; } if (num >= 400) {result[C]++;result[D]++;num -= 400; } result[C] += (num/100); num %= 100; if (num >= 90) {result[X]++;result[C]++;num -= 90; } if (num >=50) {result[L]++;num -= 50; } if (num >= 40) {result[X]++;result[L]++;num -= 40; } result[X] += (num/10); num %= 10; if (num >= 9) {result[I]++;result[X]++;num -= 9; } if (num >= 5) {result[V]++;num -= 5; } if (num >= 4) {result[I]++;result[V]++;num -= 4; } result[I] += num;} int main(void){ FILE *fin, *fout; int N, i; fin = fopen("preface.in", "r"); fout = fopen("preface.out", "w"); fscanf(fin, "%d", &N); memset(result, 0, 7*sizeof(int)); for (i=1; i<=N; i++) {CalculateLetters(i); } for (i=I; i<=M; i++) {if (result[i] != 0){ fprintf(fout, "%c %d\n", letter_map[i], result[i]);} } return 0;}
- 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
- 主定理
- v5教程--表达式
- Web Api --智能Api接口
- 经济系统总纲
- 求一个字符串s的最大连续递增数字子串
- Preface Numbering
- matlab 插值函数
- 矩形旋转碰撞,OBB方向包围盒算法实现
- JUnit integration test with Spring
- float数据的一个问题
- matlab绘制平滑曲线
- RS232应用----电功率计
- 游戏设计初探
- JavaScript 对象和数组