結果

問題 No.518 ローマ数字の和
ユーザー ooh_20ooh_20
提出日時 2017-06-02 12:09:58
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 4,874 bytes
コンパイル時間 934 ms
コンパイル使用メモリ 112,024 KB
実行使用メモリ 24,156 KB
最終ジャッジ日時 2023-10-21 20:43:03
合計ジャッジ時間 2,291 ms
ジャッジサーバーID
(参考情報)
judge10 / judge9
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
24,152 KB
testcase_01 AC 26 ms
24,152 KB
testcase_02 AC 26 ms
24,152 KB
testcase_03 AC 26 ms
24,156 KB
testcase_04 AC 25 ms
24,152 KB
testcase_05 AC 26 ms
24,152 KB
testcase_06 AC 26 ms
24,156 KB
testcase_07 AC 26 ms
24,148 KB
testcase_08 AC 25 ms
24,152 KB
testcase_09 AC 26 ms
24,152 KB
testcase_10 AC 26 ms
24,156 KB
testcase_11 AC 26 ms
24,156 KB
testcase_12 AC 25 ms
24,152 KB
testcase_13 AC 26 ms
24,156 KB
testcase_14 AC 26 ms
24,148 KB
testcase_15 AC 26 ms
24,152 KB
testcase_16 AC 26 ms
24,152 KB
testcase_17 AC 26 ms
24,152 KB
testcase_18 AC 26 ms
24,152 KB
testcase_19 AC 26 ms
24,152 KB
testcase_20 AC 25 ms
24,152 KB
testcase_21 AC 26 ms
24,148 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;

namespace No._518
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = int.Parse(Console.ReadLine());
            string[] roma = Console.ReadLine().Split();
            Program program = new Program();
            int sum = 0;

            for (int i = 0; i < N; i++)
            {
                sum += program.CharToNum(roma[i].ToCharArray());
            }

            string ans = program.NumToChar(sum);

            Console.WriteLine(ans);
        }

        public int CharToNum(char[] roma)
        {
            int result = 0;

            Array.Reverse(roma);

            for (int i = 0; i < roma.Length; i++)
            {
                switch (roma[i])
                {
                    case 'M':
                        result += 1000;
                        break;

                    case 'D':
                        result += 500;
                        break;

                    case 'C':
                        if (i != 0)
                        {
                            if (roma[i - 1] == 'M' || roma[i - 1] == 'D')
                            {
                                result -= 100;
                                break;
                            }
                        }
                        result += 100;
                        break;

                    case 'L':
                        result += 50;
                        break;

                    case 'X':
                        if (i != 0)
                        {
                            if (roma[i - 1] == 'L' || roma[i - 1] == 'C')
                            {
                                result -= 10;
                                break;
                            }
                        }
                        result += 10;
                        break;

                    case 'V':
                        result += 5;
                        break;

                    case 'I':
                        if (i != 0)
                        {
                            if (roma[i - 1] == 'V' || roma[i - 1] == 'X')
                            {
                                result -= 1;
                                break;
                            }
                        }
                        result += 1;
                        break;
                }
            }

            return result;
        }

        public string NumToChar(int number)
        {
            if (number > 3999)
            {
                return "ERROR";
            }

            string result = "";

            if (number >= 1000)
            {
                int n = number / 1000;
                number %= 1000;

                while (n > 0)
                {
                    result += "M";
                    n--;
                }
            }

            if (number >= 100)
            {
                int n = number / 100;
                number %= 100;

                if (n == 9)
                {
                    result += "CM";
                    n -= 9;
                }
                else if (8 >= n && n >= 5)
                {
                    result += "D";
                    n -= 5;
                }
                else if (n == 4)
                {
                    result += "CD";
                    n -= 4;
                }

                while (n > 0)
                {
                    result += "C";
                    n--;
                }
            }

            if (number >= 10)
            {
                int n = number / 10;
                number %= 10;

                if (n == 9)
                {
                    result += "XC";
                    n -= 9;
                }
                else if (8 >= n && n >= 5)
                {
                    result += "L";
                    n -= 5;
                }
                else if (n == 4)
                {
                    result += "XL";
                    n -= 4;
                }

                while (n > 0)
                {
                    result += "X";
                    n--;
                }
            }

            if (number >= 1)
            {
                if (number == 9)
                {
                    result += "IX";
                    number -= 9;
                }
                else if (8 >= number && number >= 5)
                {
                    result += "V";
                    number -= 5;
                }
                else if (number == 4)
                {
                    result += "IV";
                    number -= 4;
                }

                while (number > 0)
                {
                    result += "I";
                    number--;
                }
            }

            return result;
        }
    }
}
0