結果
問題 | No.518 ローマ数字の和 |
ユーザー | yuya178 |
提出日時 | 2017-06-11 23:08:27 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 56 ms / 2,000 ms |
コード長 | 6,160 bytes |
コンパイル時間 | 2,250 ms |
コンパイル使用メモリ | 79,092 KB |
実行使用メモリ | 50,676 KB |
最終ジャッジ日時 | 2024-09-24 16:37:11 |
合計ジャッジ時間 | 4,397 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 53 ms
50,552 KB |
testcase_01 | AC | 53 ms
50,672 KB |
testcase_02 | AC | 54 ms
50,240 KB |
testcase_03 | AC | 54 ms
50,268 KB |
testcase_04 | AC | 53 ms
50,432 KB |
testcase_05 | AC | 55 ms
50,540 KB |
testcase_06 | AC | 54 ms
50,316 KB |
testcase_07 | AC | 54 ms
50,548 KB |
testcase_08 | AC | 54 ms
50,304 KB |
testcase_09 | AC | 52 ms
50,192 KB |
testcase_10 | AC | 54 ms
50,160 KB |
testcase_11 | AC | 53 ms
50,064 KB |
testcase_12 | AC | 53 ms
50,376 KB |
testcase_13 | AC | 54 ms
50,180 KB |
testcase_14 | AC | 54 ms
50,528 KB |
testcase_15 | AC | 54 ms
50,468 KB |
testcase_16 | AC | 56 ms
50,560 KB |
testcase_17 | AC | 56 ms
50,520 KB |
testcase_18 | AC | 55 ms
50,676 KB |
testcase_19 | AC | 54 ms
50,580 KB |
testcase_20 | AC | 55 ms
50,544 KB |
testcase_21 | AC | 53 ms
50,268 KB |
ソースコード
import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.InputMismatchException; import java.io.OutputStream; import java.util.StringTokenizer; import java.io.BufferedReader; import java.io.InputStreamReader; /** * Built using CHelper plug-in * Actual solution is at the top */ public class Main { public static void main(String[] args) { InputStream inputStream = System.in; OutputStream outputStream = System.out; InputReader in = new InputReader(inputStream); PrintWriter out = new PrintWriter(outputStream); Task solver = new Task(); solver.solve(1, in, out); out.close(); } static class Task { public void solve(int testNumber, InputReader in, PrintWriter out) { int N = in.nextInt(); String a[] = new String[N]; int b[] = new int[N]; for(int i=0; i<N; i++){ a[i] = in.next(); for(int j=0; j<a[i].length(); j++){ if(j==a[i].length()-1){ if(a[i].charAt(j)=='I') b[i]+=1; else if(a[i].charAt(j)=='V') b[i]+=5; else if(a[i].charAt(j)=='X') b[i]+=10; else if(a[i].charAt(j)=='L') b[i]+=50; else if(a[i].charAt(j)=='C') b[i]+=100; else if(a[i].charAt(j)=='D') b[i]+=500; else if(a[i].charAt(j)=='M') b[i]+=1000; } else{ if(a[i].charAt(j)=='I'){ if(a[i].charAt(j+1)=='V'){ b[i]+=4; j++; } else if(a[i].charAt(j+1)=='X'){ b[i]+=9; j++; } else b[i]+=1; } else if(a[i].charAt(j)=='X'){ if(a[i].charAt(j+1)=='L'){ b[i]+=40; j++; } else if(a[i].charAt(j+1)=='C'){ b[i]+=90; j++; } else b[i]+=10; } else if(a[i].charAt(j)=='C'){ if(a[i].charAt(j+1)=='D'){ b[i]+=400; j++; } else if(a[i].charAt(j+1)=='M'){ b[i]+=900; j++; } else b[i]+=100; } else if(a[i].charAt(j)=='V') b[i]+=5; else if(a[i].charAt(j)=='L') b[i]+=50; else if(a[i].charAt(j)=='D') b[i]+=500; else if(a[i].charAt(j)=='M') b[i]+=1000; } } } int res=0; for(int i=0; i<N;i++){ res+=b[i]; } if(res>3999){ out.println("ERROR"); return; } int v; while(res>0){ if(res>=1000){ v=res/1000; if(v<4){ res -= v*1000; for(int i=0; i<v; i++) out.print("M"); } } if(res>=100){ v=res/100; res -= v*100; if(v<4){ for(int i=0; i<v; i++) out.print("C"); } else if(v==4) out.print("CD"); else if(v<9){ out.print("D"); for(int i=0; i<v-5; i++){ out.print("C"); } } else out.print("CM"); } if(res>=10){ v=res/10; res -= v*10; if(v<4){ for(int i=0; i<v; i++) out.print("X"); } else if(v==4) out.print("XL"); else if(v<9){ out.print("L"); for(int i=0; i<v-5; i++){ out.print("X"); } } else out.print("XC"); } if(res>=1){ v=res/1; res -= v*1; if(v<4){ for(int i=0; i<v; i++) out.print("I"); } else if(v==4) out.print("IV"); else if(v<9){ out.print("V"); for(int i=0; i<v-5; i++){ out.print("I"); } } else out.print("IX"); } } } } static class InputReader { public BufferedReader reader; public StringTokenizer tokenizer; public InputReader(InputStream stream) { reader = new BufferedReader(new InputStreamReader(stream), 32768); tokenizer = null; } public String next() { while (tokenizer == null || !tokenizer.hasMoreTokens()) { try { tokenizer = new StringTokenizer(reader.readLine()); } catch (IOException e) { throw new RuntimeException(e); } } return tokenizer.nextToken(); } public int nextInt() { return Integer.parseInt(next()); } } }