Last active
November 26, 2016 22:35
-
-
Save Soulstorm50/f8adaf4b7ef7c798ab7cf4fbdf8e9abe to your computer and use it in GitHub Desktop.
C# Массив римских чисел
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace ConsoleApplication21 | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
string[] digitArray = new string[4000]; | |
for (int i = 1; i < 4000; i++) | |
digitArray[i] = ConvertToRoman(i); | |
for (int i = 1; i < 4000; i++) | |
Console.WriteLine(digitArray[i]); | |
} | |
static string ConvertToRoman(int value) | |
{ | |
string result = ""; | |
while (value > 0) | |
{ | |
int valuePart = 0; | |
int valueBase = 0; | |
if (value >= 1000) | |
valueBase = 1000; | |
else if (value >= 100) | |
valueBase = 100; | |
else if (value >= 10) | |
valueBase = 10; | |
else | |
valueBase = 1; | |
valuePart = value / valueBase * valueBase; | |
value -= valuePart; | |
if (valuePart / valueBase == 9) | |
{ | |
if (valueBase == 100) | |
result = result + "CM"; | |
else if (valueBase == 10) | |
result = result + "XC"; | |
else | |
result = result + "IX"; | |
} | |
else if (valuePart / valueBase == 4) | |
{ | |
if (valueBase == 100) | |
result = result + "CD"; | |
else if (valueBase == 10) | |
result = result + "XL"; | |
else | |
result = result + "IV"; | |
} | |
else | |
{ | |
if (valueBase == 1000) | |
{ | |
while (valuePart > 0) | |
{ | |
result = result + "M"; | |
valuePart -= 1000; | |
} | |
} | |
else if (valueBase == 100) | |
{ | |
if (valuePart >= 500) | |
{ | |
result = result + "D"; | |
valuePart -= 500; | |
} | |
while (valuePart > 0) | |
{ | |
result = result + "C"; | |
valuePart -= 100; | |
} | |
} | |
else if (valueBase == 10) | |
{ | |
if (valuePart >= 50) | |
{ | |
result = result + "L"; | |
valuePart -= 50; | |
} | |
while (valuePart > 0) | |
{ | |
result = result + "X"; | |
valuePart -= 10; | |
} | |
} | |
else//valueBase == 1 | |
{ | |
if (valuePart >= 5) | |
{ | |
result = result + "V"; | |
valuePart -= 5; | |
} | |
while (valuePart > 0) | |
{ | |
result = result + "I"; | |
valuePart -= 1; | |
} | |
} | |
} | |
} | |
return result; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment