【leetcode】12. Integer to Roman(C)

Description:

Difficulty
Medium
358
1351
Discuss (387)
Favorite
Share
ACCEPTED
171,689

SUBMISSIONS
358,141

Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.

Example1:

Input: 3
Output: “III”

Example2:

Input: 4
Output: “IV”

Example3:

Input: 9
Output: “IX”

Example4:

Input: 58
Output: “LVIII”
Explanation: L = 50, V = 5, III = 3.

Example5:

Input: 1994
Output: “MCMXCIV”
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

[ 题目链接 ]

提交代码:

char* intToRoman(int num) {
   	int tmp = num;
	int i, j,pos=0;
	char* roman=(char*)malloc(50*sizeof(char));
	int thousands, hundreds, tens, units;

	thousands = tmp / 1000;
	tmp = tmp - thousands * 1000;
	hundreds = tmp / 100;
	tmp = tmp - hundreds * 100;
	tens = tmp / 10;
	tmp = tmp - tens * 10;
	units = tmp;

	if (thousands != 0)
	{
		for (i = 0; i < thousands; i++)
		{
			roman[pos++] = 'M';  //pos-1为最后一个字符所在位置
		}
	}
	if (hundreds != 0)
	{
		if (hundreds == 9)
		{
			roman[pos++] = 'C';
			roman[pos++] = 'M';
		}
		else if (hundreds == 4)
		{
			roman[pos++] = 'C';
			roman[pos++] = 'D';
		}
		else if (hundreds == 5)
			roman[pos++] = 'D';
		else if(hundreds>5)
		{
			roman[pos++] = 'D';
			hundreds -= 5;
			for (i = 0; i < hundreds; i++)
			{
				roman[pos++] = 'C';
			}
		}
		else
		{
			for (i = 0; i < hundreds; i++)
			{
				roman[pos++] = 'C';
			}
		}
	}
	if (tens != 0)
	{
		if (tens == 9)
		{
			roman[pos++] = 'X';
			roman[pos++] = 'C';
		}
		else if (tens == 4)
		{
			roman[pos++] = 'X';
			roman[pos++] = 'L';
		}
		else if (tens == 5)
			roman[pos++] = 'L';
		else if (tens>5)
		{
			roman[pos++] = 'L';
			tens -= 5;
			for (i = 0; i < tens; i++)
			{
				roman[pos++] = 'X';
			}
		}
		else
		{
			for (i = 0; i < tens; i++)
			{
				roman[pos++] = 'X';
			}
		}
	}
	if (units != 0)
	{
		if (units == 9)
		{
			roman[pos++] = 'I';
			roman[pos++] = 'X';
		}
		else if (units == 4)
		{
			roman[pos++] = 'I';
			roman[pos++] = 'V';
		}
		else if (units == 5)
			roman[pos++] = 'V';
		else if (units>5)
		{
			roman[pos++] = 'V';
			units -= 5;
			for (i = 0; i < units; i++)
			{
				roman[pos++] = 'I';
			}
		}
		else
		{
			for (i = 0; i < units; i++)
			{
				roman[pos++] = 'I';
			}
		}
	}
	roman[pos] = '\0';
	return roman; 
}

运行结果:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/AXIMI/article/details/83104734