问题描述:

Possible Duplicate:How to convert integer value to Roman numeral string?

Back in my college days, when learning C and all i had come across a question of representing a year in its corresponding Roman Numeral form. There was no solution in that text, as it was among some extra questions to ponder. All i could think of was using modulus operator and a bunch of ifs. I was wondering if some one could give me a proper solution. A simple algorithm or explanation of the logic used would be appreciated.

Here's logic for numbers less than 4,000. (See below for what to do above that.) There's a basic, 4-step algorithm that is applied at each level of magnitude.

Determine the number of thousands in the number: floor(number/1000). Output that many "M"s and subtract that many thousands from the number. At this point, the number is less than 1,000.

If the number is >= 900, output "CM" and subtract 900.

If the number is >= 500, output "D" and subtract 500.

If the number is >= 400, output "CD" and subtract 400.

At this point, the number is guaranteed to be < 400. We follow a similar pattern to reduce the number to less than 40:

Determine the number of hundreds in the number, output that many "C"s and subtract that many hundreds from the number. At this point, the number is less than 100.

If the number is >= 90, output "XC" and subtract 900.

If the number is >= 50, output "L" and subtract 50.

If the number is >= 40, output "XL" and subtract 40.

At this point, the number is guaranteed to be < 40. We repeat exactly the same logic using "X", "IX", "V", and "IV". We finish by using the number (guaranteed to be < 4) as the count of how many "I"s to output.

For larger numbers, the logic is still the same, we just use the standard symbols with bars on top. Each bar represents 1,000 times the value without that bar. (So V with a bar is 5,000, etc.)

您可能感兴趣的文章：

- javascript - Cannot display Tiny MCE Editor inside update panel C#
- jquery - CakePHP Submit Search Field during entry threw Ajax
- c++ - Can a multithreaded function use a static array to share data between threads?
- c# - Client receiving packet only on close
- windows - How do i check how long a file has been in the Recycle Bin?
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- Forward to a payment-gateway together with POST data using cURL (or any other PHP server side solution)
- WCF in Winforms app - is it always single-threaded?

随机阅读：

**推荐内容**-

**热点内容**-
- php - Reading a specific line from a text file
- clr - Any implementation of an Unrolled Linked List in C#?
- Finding Hudson Log Files
- Forward to a payment-gateway together with POST data using cURL (or any other PHP server side solution)
- WCF in Winforms app - is it always single-threaded?
- git svn - git svn fetch does not fetch a Subversion commit message modified after initial clone
- java me - Why I am getting the bad length exception when I am running this application?
- java - How to get string.format to complain at compile time
- ruby on rails - Trigger observer of parent class on change
- python - Issue with URL pattern in Django with webmonkey tutorial