1

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.

4

1 回答 1

5

这是小于 4,000 的数字的逻辑。(有关在此之上做什么,请参见下文。)有一个基本的 4 步算法适用于每个幅度级别。

  1. 确定数字中的千位数:floor(number/1000)。输出那么多“M”并从数字中减去几千。此时,人数不足1000人。

  2. 如果数字 >= 900,则输出“CM”并减去 900。

  3. 如果数字 >= 500,则输出“D”并减去 500。

  4. 如果数字 >= 400,则输出“CD”并减去 400。

此时,保证数量<400。我们遵循类似的模式将数量减少到小于40:

  1. 确定数字中的百位数,输出那么多“C”并从数字中减去那么多百。此时,人数不足 100 人。

  2. 如果数字 >= 90,则输出“XC”并减去 900。

  3. 如果数字 >= 50,则输出“L”并减去 50。

  4. 如果数字 >= 40,则输出“XL”并减去 40。

在这一点上,数字保证小于 40。我们使用“X”、“IX”、“V”和“IV”重复完全相同的逻辑。最后,我们使用数字(保证 < 4)作为要输出多少个“I”的计数。

对于较大的数字,逻辑仍然相同,我们只是使用顶部带有条形的标准符号。每个条表示没有该条的值的 1,000 倍。(所以带条的 V 是 5,000 等)

于 2011-06-29T06:34:05.217 回答