-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathIntegertoRoman.cs
executable file
·77 lines (73 loc) · 4.62 KB
/
IntegertoRoman.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Source : https://leetcode.com/problems/integer-to-roman/
// Author : codeyu
// Date : 10/4/16
/***************************************************************************************
*
* Given an integer, convert it to a roman numeral.
*
* Input is guaranteed to be within the range from 1 to 3999.
*
**********************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
namespace Algorithms
{
public class Solution012
{
public static string IntToRoman(int num)
{
if(num < 0 || num > 3999)
{
return "";
}
string str = "";
string [] symbol = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
int [] value = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
for(int i=0;num!=0;i++)
{
while(num >= value[i])
{
num -= value[i];
str += symbol[i];
}
}
return str;
}
public static string IntToRoman2(int num)
{
char[] symbol = {'I','V','X','L','C','D','M'};
StringBuilder roman = new StringBuilder();
int scale = 1000;
int p = 6;
while(num > 0)
{
int digit = num / scale;
if(digit > 0 && digit < 10)
{
roman.Append(RomanStr(digit, symbol, p));
}
num = num % scale;
scale /= 10;
p -= 2;
}
return roman.ToString();
}
private static string RomanStr(int digit, char[] symbol, int p)
{
StringBuilder roman = new StringBuilder();
if(digit <= 3) roman.Append(symbol[p], digit);
else if(digit == 4){
roman.Append(symbol[p]);
roman.Append(symbol[p+1]);
}else if(digit <= 8){
roman.Append(symbol[p+1]);
roman.Append(symbol[p], digit-5);
}else if(digit == 9){
roman.Append(symbol[p]);
roman.Append(symbol[p+2]);
}
return roman.ToString();
}
}
}