Leetcode 罗马数字转整数 C#

自己没想出来,看着网上JAVA改的,各位大佬勿喷,多多指教,C#的答案太少了,权当整理吧

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 罗马数字转成整数
{
    class Program
    {
        static void Main(string[] args)
        {
            int num = Romantoint(Console.ReadLine());
            Console.WriteLine(num);
            Console.ReadKey();
        }

        public static int Romantoint(String s)
        {

            int result = 0;
            char[] chars = s.ToCharArray();
            for (int i = 0; i < chars.Length; i++)
            {
                switch (chars[i])
                {
                    case 'M':
                        result += 1000;
                        break;
                    case 'D':
                        result += 500;
                        break;
                    case 'C':
                        if (i + 1 < chars.Length && chars[i + 1] == 'M')
                        {//确保数组不越界以及CM的特殊情况,以下几种情况同理
                            result += 900;
                            i++;//i++是为了跳过下一个字符,直接去寻找CM下一个字符
                        }
                        else if (i + 1 < chars.Length && chars[i + 1] == 'D')
                        {
                            result += 400;
                            i++;//同理
                        }
                        else
                        {
                            result += 100;
                        }
                        break;
                    case 'L':
                        result += 50;
                        break;
                    case 'X':
                        if (i + 1 < chars.Length && chars[i + 1] == 'L')
                        {
                            result += 40;
                            i++;
                        }
                        else if (i + 1 < chars.Length && chars[i + 1] == 'C')
                        {
                            result += 90;
                            i++;
                        }
                        else
                        {
                            result += 10;
                        }
                        break;
                    case 'V':
                        result += 5;
                        break;
                    case 'I':
                        if (i == chars.Length - 1)
                        {//确保数组不越界
                            result += 1;
                            break;
                        }
                        if (chars[i + 1] == 'V')
                        {
                            result += 4;
                            i++;
                        }
                        else if (chars[i + 1] == 'X')
                        {
                            result += 9;
                            i++;
                        }
                        else
                        {
                            result += 1;
                        }
                        break;
                    default:
                        break;
                }
            }
            return result;
        }
    }
}

其实就是1\4\5\9\10,然后十百千,这样罗列出来,switch-case一下应该也能出来,自己能力有限,大家见谅~

转载地址:https://blog.csdn.net/myone_two/article/details/80335330

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值