結果

問題 No.518 ローマ数字の和
ユーザー ooh_20ooh_20
提出日時 2017-06-02 12:01:17
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 4,874 bytes
コンパイル時間 1,436 ms
コンパイル使用メモリ 111,688 KB
実行使用メモリ 24,176 KB
最終ジャッジ日時 2023-10-21 20:42:31
合計ジャッジ時間 2,247 ms
ジャッジサーバーID
(参考情報)
judge10 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 26 ms
24,172 KB
testcase_02 AC 26 ms
24,172 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 25 ms
24,172 KB
testcase_06 AC 26 ms
24,176 KB
testcase_07 WA -
testcase_08 AC 25 ms
24,172 KB
testcase_09 WA -
testcase_10 AC 25 ms
24,176 KB
testcase_11 AC 25 ms
24,176 KB
testcase_12 AC 25 ms
24,172 KB
testcase_13 AC 25 ms
24,176 KB
testcase_14 WA -
testcase_15 AC 25 ms
24,172 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 25 ms
24,172 KB
testcase_19 AC 25 ms
24,172 KB
testcase_20 AC 25 ms
24,172 KB
testcase_21 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 "ERRER";
            }

            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 += "V";
                    number--;
                }
            }

            return result;
        }
    }
}
0