整数到罗马格式

时间:2014-03-13 22:59:46

标签: c# asp.net

我想从0-50获得罗马数字。我只是在文本框上写下整数,然后按下按钮,我想在标签中加入罗马格式。

我已经编写了代码,它在0-50个数字中运行良好。

但是,我认为我所拥有的解决方案并非最佳。

任何人都可以帮助我,我怎样才能让它更优化。

int number = Convert.ToInt32( tb_input.Text);
            StringBuilder sb = new StringBuilder();      
            sb= IntToRoman(number, sb);



       Label1.Text =sb.ToString();


public StringBuilder IntToRoman(int number, StringBuilder sb)
    {
        int flag = 0;
        if (number >= 50 && flag==0)
        {
            sb.Append("L");
            IntToRoman(number - 50, sb);
            flag = 1;
        }
        if (number >= 10 && flag == 0)
        {
            sb.Append("X");
            IntToRoman(number - 10, sb);
            flag = 1;

        }
        if (number >= 9 && flag == 0)
        {
            sb.Append("IX");
            IntToRoman(number - 9, sb);
            flag = 1;
        }

        if (number >= 5 && flag == 0)
        {
            sb.Append("V");
            IntToRoman(number - 5, sb);
            flag = 1;
        }
        if (number >= 4 && flag == 0)
        {
            sb.Append("IV-");
            IntToRoman(number - 4, sb);
            flag = 1;
        }

        if (number >= 1 && flag == 0)
        {
            sb.Append("I");
            IntToRoman(number - 1, sb);
            flag = 1;
        }

        if (number ==0)
        {            
            return sb;

        }
        return sb;
    }

6 个答案:

答案 0 :(得分:3)

非常简单的代码,适用于任何数字......

    public static List<string> romanNumerals = new List<string>() { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I" };
    public static List<int> numerals = new List<int>() { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };

    public static string ToRomanNumeral(int number)
    {
        var romanNumeral = string.Empty;
        while (number > 0)
        {
            // find biggest numeral that is less than equal to number
            var index = numerals.FindIndex(x => x <= number);
            // subtract it's value from your number
            number -= numerals[index];
            // tack it onto the end of your roman numeral
            romanNumeral += romanNumerals[index];
        }
        return romanNumeral;
    }

...用法

ToRomanNumeral(58) = 'LVIII'
ToRomanNumeral(2014) = 'MMXIV'

答案 1 :(得分:2)

试试这个:

它适用于数字1-3999;)如果您需要更多数字,您可以适应您的需要;)

public StringBuilder IntToRoman(int number, StringBuilder sb)
{
    string line = number.ToString();
    string[] numbers = { "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM", "M", "MM", "MMM" };
    int a = 1;
    for (int i = line.Length - 1; i >= 0; i--)
    {
        string t = line.Substring(i, 1);
        int t1 = int.Parse(t) * a;
        if (t1 < 10 && t1 > 0)
            sb.Insert(0, numbers[t1 - 1]);
        if (t1 >= 10 && t1 < 100)
            sb.Insert(0, numbers[9 + (int.Parse(t) - 1)]);
        if (t1 >= 100 && t1 < 1000)
            sb.Insert(0, numbers[18 + (int.Parse(t) - 1)]);
        if (t1 >= 1000)
            sb.Insert(0, numbers[27 + (int.Parse(t) - 1)]);
        a *= 10;
    }
    Console.WriteLine(sb.ToString());
}

;)我希望这有效 这里的帖子在我的博客中有解释:

http://topblogcoder.com/codeeval-c-roman-numerals/

答案 2 :(得分:1)

这对我很有用:

Func<int, string> toRoman = x =>
{
    var numerals = new []
    {
        new { text = "L", value = 50, },
        new { text = "XL", value = 40, },
        new { text = "X", value = 10, },
        new { text = "IX", value = 9, },
        new { text = "V", value = 5, },
        new { text = "IV", value = 4, },
        new { text = "I", value = 1, },
    };

    return
        numerals.Aggregate(new { text = "", value = x }, (a, n) =>
        {
            var text = a.text;
            var value = a.value;
            while (value >= n.value)
            {
                text += n.text;
                value -= n.value;
            }
            return new { text, value };
        }).text;
};

例如:

toRoman(49) == "XLIX"
toRoman(73) == "LXXIII"
toRoman(8) == "VIII"

我认为使用StringBuilder几乎没有意义,因为所涉及的值不太可能通过使用字符串连接来解决重大性能问题。如果StringBuilder被认为是重要的,那么加入{{1}}并不困难。

答案 3 :(得分:0)

using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class Form1
{
    int[] indx = {
        1,
        2,
        3,
        4,
        5,
        10,
        50,
        100,
        500,
        1000
        // initialize array of integers 
    };
    string[] row = {
        "I",
        "II",
        "III",
        "IV",
        "V",
        "X",
        "L",
        "C",
        "D",
        "M"
        //Carasponding roman letters in for the numbers in the array
    };
        // integer to indicate the position index for link two arrays 
    int limit = 9;
        //string to store output
    string output = "";
    private void Button1_Click(System.Object sender, System.EventArgs e)
    {
        int num = 0;
        // stores the input 
        output = "";
        // clear output before processing
        num = Convert.ToInt32(txt1.Text);
        // get integer value from the textbox
        //Loop until the value became 0
        while (num > 0) {
            num = find(num);
            //call function for processing
        }
        txt2.Text = output;
        // display the output in text2
    }
    public int find(int Num)
    {
        int i = 0;
        // loop variable initialized with 0
        //Loop until the indx(i).value greater than or equal to num
        while (indx(i) <= Num) {
            i += 1;
        }
        // detemine the value of limit depends on the itetration
        if (i != 0) {
            limit = i - 1;
        } else {
            limit = 0;
        }
        output = output + row(limit);
        //row(limit) is appended with the output
        Num = Num - indx(limit);
        // calculate next num value
        return Num;
        //return num value for next itetration 
    }
}

答案 4 :(得分:0)

如果您只是在寻找一种将整数转换为0-59之间的罗马数字的解决方案,那么这里是一种简单快捷的解决方案。

解决方案

const getRoman = num => {
  if (num===0) {
    return "";
  }
  let result = "";
  let n = num%10;
  let rest = Math.floor(num/10);
  if (n === 0) {
    getRoman(rest);
  } else if (n<4) {
    for (let i=0;i<n; i++) {
      result = result + "I";
    }
  } else if (n === 4) {
    result = result + "IV";
  } else if (n === 5) {
    result = result + "V";
  } else if (n < 9) {
    result = result + "V";
    for (let l = 6; l < 9; l++) {
      result = result + "I";
    }
  } else if (n === 9) {
    result = result+"IX";
  } else {
    result = result + "V";
    n = n-5;
    for (let i=0; i<n; i++) {
      result = result+"I";
    }
  }
  if (rest === 0) {
    return result;
  } else if (rest<4) {
    for (let j = 0; j < rest; j++) {
      result = "X" + result;
    }
  } else if (rest === 4) {
    result = "XL" + result;
  } else {
    result = "L" + result;
  }
  return result;
}

示例输入

console.log(getRoman(1))
console.log(getRoman(50))
console.log(getRoman(59))
console.log(getRoman(0))
console.log(getRoman(42))
console.log(getRoman(23))
console.log(getRoman(29))
console.log(getRoman(34))

输出

I
L
LIX

XLII
XXIII
XXIX
XXXIV

答案 5 :(得分:0)

这将处理带有等同罗马字母的较大小数(最多1.000.000)。

/// <summary>
/// Convert decimals to roman numerals
/// </summary>
public static class RomanNumerals
{
    /// <summary>
    /// Dictionary of roman numbers and their equavilant decimals.
    /// IMPORTANT: Keep the order from larger to smaller
    /// </summary>
    public static Dictionary<uint, string> RomanNumbers =
        new Dictionary<uint, string>
        {
            { 1000000, "M̅" },
            { 900000, "C̅M̅" },

            { 500000, "D̅" },
            { 400000, "C̅D̅" },

            { 100000, "C̅" },
            { 90000, "X̅C̅" },

            { 50000, "L̅" },
            { 40000, "X̅L̅" },

            { 10000, "X̅" },
            { 9000, "I̅X̅" },

            { 5000, "V̅" },
            { 4000, "I̅V̅" },

            { 1000, "M" },
            { 900, "DM" },

            { 500, "D" },
            { 400, "CD" },

            { 100, "C" },
            { 90, "XC" },

            { 50, "L" },
            { 40, "XL" },

            { 10, "X" },
            { 9, "IX" },

            { 5, "V" },
            { 4, "IV" },

            { 1, "I" },
        };

    /// <summary>
    /// Convert decimal number to roman number
    /// </summary>
    /// <param name="number">unsigned number</param>
    /// <returns>Roman number</returns>
    public static string ToRoman(this uint number)
    {
        var romanNum = string.Empty;

        while (number > 0)
        {
            // Make sure to order from larger to smaller
            var item = RomanNumbers
                       .OrderByDescending(x => x.Key)
                       .First(x => x.Key <= number);
            romanNum += item.Value;
            number -= item.Key;
        }

        return romanNum;
    }
}

用法:

uint number = 1000001;
number.ToRoman(); // M̅I

number = 999999;
number.ToRoman(); // C̅M̅X̅C̅I̅X̅DMXCIX

number = 500009;
number.ToRoman(); // D̅IX

请参阅: