0123456789
Time Limit: 1000ms
Memory Limit: 65536KB
Prev
Submit
Status
Statistics
Discuss
Next
Font Size:
+
-
Type: None Graph Theory 2-SAT Articulation/Bridge/Biconnected Component Cycles/Topological Sorting/Strongly Connected Component Shortest Path Bellman Ford Dijkstra/Floyd Warshall Euler Trail/Circuit Heavy-Light Decomposition Minimum Spanning Tree Stable Marriage Problem Trees Directed Minimum Spanning Tree Flow/Matching Graph Matching Bipartite Matching Hopcroft–Karp Bipartite Matching Weighted Bipartite Matching/Hungarian Algorithm Flow Max Flow/Min Cut Min Cost Max Flow DFS-like Backtracking with Pruning/Branch and Bound Basic Recursion IDA* Search Parsing/Grammar Breadth First Search/Depth First Search Advanced Search Techniques Binary Search/Bisection Ternary Search Geometry Basic Geometry Computational Geometry Convex Hull Pick's Theorem Game Theory Green Hackenbush/Colon Principle/Fusion Principle Nim Sprague-Grundy Number Matrix Gaussian Elimination Matrix Exponentiation Data Structures Basic Data Structures Binary Indexed Tree Binary Search Tree Hashing Orthogonal Range Search Range Minimum Query/Lowest Common Ancestor Segment Tree/Interval Tree Trie Tree Sorting Disjoint Set String Aho Corasick Knuth-Morris-Pratt Suffix Array/Suffix Tree Math Basic Math Big Integer Arithmetic Number Theory Chinese Remainder Theorem Extended Euclid Inclusion/Exclusion Modular Arithmetic Combinatorics Group Theory/Burnside's lemma Counting Probability/Expected Value Others Tricky Hardest Unusual Brute Force Implementation Constructive Algorithms Two Pointer Bitmask Beginner Discrete Logarithm/Shank's Baby-step Giant-step Algorithm Greedy Divide and Conquer Dynamic Programming
Tag it!
罗马数字是欧洲在阿拉伯数字(实际上是印度数字)传入之前使用的一种数码,现在应用较少.它的产生晚于中国甲骨文中的数码,更晚于埃及人的十进位数字.但是,它的产生标志着一种古代文明的进步.
罗马数字用7个拉丁字母IVXLCDM和组数规则来表示数.
I = 1
V = 5
X = 10
L = 50
C = 100
D = 500
M = 1000
组数规则有以下一些
最多3个可以表示为10
n的数字(I,X,C,M)可以连续放在一起,表示它们的和: II=2,CC=200,III=3,CCC=300
可表示为5*10
n的字符(V,L,D)从不连续出现。
字符一般以值递减的顺序接连出现:
CCLXVIII = 100+100+50+10+5+1+1+1 = 268
但也有例外,有时,一个可表示为10
n的数出现在一个比它大的数前.在这种情况下,数值等于后面的那个数减去前面的那个数:
IV = 4
IX = 9
XL = 40
不过,像XD,IC和XM这样的表达是非法的,因为前面的数比后面的数小太多,每一个用来做减去的数字必须保证他后面紧跟着的数字不超过它的10倍.对于XD(490的错误表达),应该写成 CDXC;对于IC(99的错误表达),可以写成XCIX;对于XM(990的错误表达),应该写成CMXC.
现在要求你编写程序把实现阿拉伯数字到罗马数字的转换.
Input
输入文件包含多组测试数据,每组测试数据一行,给出一个阿拉伯数字表示下的正整数,该数不超过3000.
Output
对于每一组测试数据,对应输出一行,为阿拉伯数字表示.
Sample Input
Sample Output
Source
2009年北京师范大学新生程序设计竞赛热身赛(第二场)
Author
Huang Kun @ BNU
#include<stdio.h>
int main()
{
int n,t;
while(~scanf("%d",&n))
{
if(n>=1000)
{
t=n/1000;
n=n-t*1000;
while(t--)
printf("M");
}
if(n>=100)
{
t=n/100;
n=n-t*100;
if(t<=3)
{
while(t--)
printf("C");
}
else if(t==4)
printf("CD");
else if(t<9)
{
t-=5;
printf("D");
while(t--)
printf("C");
}
else if(t==9)
printf("CM");
}
if(n>=10)
{
t=n/10;
n-=t*10;
if(t<=3)
{
while(t--)
printf("X");
}
else if(t==4)
printf("XL");
else if(t<9)
{
t-=5;
printf("L");
while(t--)
printf("X");
}
else if(t==9)
printf("XC");
}
if(n<10)
{
if(n<=3)
{
while(n--)
printf("I");
}
else if(n==4)
printf("IV");
else if(n<9)
{
n-=5;
printf("V");
while(n--)
printf("I");
}
else if(n==9)
printf("IX");
}
printf("\n");
}
return 0;
}