Преобразуйте заданное десятичное натуральное число в римскую систему счисления — Pascal(Паскаль)/Python(Питон)/C#(Си Шарп)

Pascal

var
a:array[1..13]of string;
b:array[1..13]of integer;
i,k,n:integer;
st,chislo:string;
begin
st:='MCMDCDCXCLXLXIXVIVI';
for i:=1 to 13 do
begin
if i mod 2<>0 then k:=1 else k:=2;
a[i]:=copy(st,1,k);
delete(st,1,k);
case i of
 1:b[i]:=1000;
 2:b[i]:=900;
 3:b[i]:=500;
 4:b[i]:=400;
 5:b[i]:=100;
 6:b[i]:=90;
 7:b[i]:=50;
 8:b[i]:=40;
 9:b[i]:=10;
 10:b[i]:=9;
 11:b[i]:=5;
 12:b[i]:=4;
 13:b[i]:=1;
end;
end;
writeln('Arabskoe chislo=');
readln(n);
i:=0;
repeat
inc(i);
while(n>=b[i]) do
begin
n:=n-b[i];
chislo:=chislo+a[i];
end;
until n=0;
writeln('Rimskoe chislo=',chislo);
end.

Python

coding = zip(
    [1000,900,500,400,100,90,50,40,10,9,5,4,1],
    ["M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"]
)
 
def decToRoman(num):
    if num <= 0 or num >= 4000 or int(num) != num:
        raise ValueError('Input should be an integer between 1 and 3999')
    result = []
    for d, r in coding:
        while num >= d:
            result.append(r)
            num -= d
    return ''.join(result)

c#

using System;
using System.Text;
					
public class Program
{
	
	public static string NumberToRoman(int number)
	{
		if (number < 0 || number > 3999)
    		throw new ArgumentException("Value must be in the range 0 - 3,999.");
		
		if (number == 0) return "N";
		
		int[] values = new int[] { 1000, 900, 500, 400, 100,90, 50, 40, 10, 9, 5, 4, 1 };
		string[] numerals = new string[]
    	{ "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
		
		
		StringBuilder result = new StringBuilder();
			
			
			for (int i = 0; i < 13; i++)
		{
    		while (number >= values[i]){
        		number -= values[i];
        		result.Append(numerals[i]);
    		}
		}
		
		return result.ToString();
	}
	
		
	public static void Main()
	{
		Console.WriteLine(NumberToRoman(19));
	}	
}

Leave a Comment

− 3 = 2