結果
問題 | No.518 ローマ数字の和 |
ユーザー |
![]() |
提出日時 | 2017-05-28 22:20:15 |
言語 | Java (openjdk 23) |
結果 |
AC
|
実行時間 | 57 ms / 2,000 ms |
コード長 | 4,458 bytes |
コンパイル時間 | 2,453 ms |
コンパイル使用メモリ | 78,904 KB |
実行使用メモリ | 50,208 KB |
最終ジャッジ日時 | 2024-09-21 15:34:56 |
合計ジャッジ時間 | 4,410 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
import java.io.IOException;import java.io.InputStream;import java.util.ArrayDeque;import java.util.NoSuchElementException;public class Main {private static FastScanner sc = new FastScanner();public static void main(String[] args) {int N = sc.nextInt();int value = 0;for(int i=0; i<N; i++) {char[] R = sc.next().toCharArray();value += romanToInt(R);// System.out.println(value);}if(value >= 4000) {System.out.println("ERROR");return;}intToRoman(value);}static int romanToInt(char[] R) {ArrayDeque<Character> chr = new ArrayDeque<>();for(int i=0; i<R.length; i++) {chr.addFirst(R[i]);}int ans = 0;while(!chr.isEmpty() && chr.peek() == 'I') {chr.poll();ans += 1;}if(!chr.isEmpty() && chr.peek() == 'V') {chr.poll();if(!chr.isEmpty() && chr.peek() == 'I') {chr.poll();ans += 4;} else {ans += 5;}}if(!chr.isEmpty() && chr.peek() == 'X') {chr.poll();if(!chr.isEmpty() && chr.peek() == 'I') {chr.poll();ans += 9;} else {chr.addFirst('X');}}while(!chr.isEmpty() && chr.peek() == 'X') {chr.poll();ans += 10;}if(!chr.isEmpty() && chr.peek() == 'L') {chr.poll();if(!chr.isEmpty() && chr.peek() == 'X') {chr.poll();ans += 40;} else {ans += 50;}}if(!chr.isEmpty() && chr.peek() == 'C') {chr.poll();if(!chr.isEmpty() && chr.peek() == 'X') {chr.poll();ans += 90;} else {chr.addFirst('C');}}while(!chr.isEmpty() && chr.peek() == 'C') {chr.poll();ans += 100;}if(!chr.isEmpty() && chr.peek() == 'D') {chr.poll();if(!chr.isEmpty() && chr.peek() == 'C') {chr.poll();ans += 400;} else {ans += 500;}}if(!chr.isEmpty() && chr.peek() == 'M') {chr.poll();if(!chr.isEmpty() && chr.peek() == 'C') {chr.poll();ans += 900;} else {chr.addFirst('M');}}while(!chr.isEmpty() && chr.peek() == 'M') {chr.poll();ans += 1000;}return ans;}static void intToRoman(int value) {int d4 = value / 1000;value %= 1000;for(int i=0; i<d4; i++) {System.out.print("M");}int d3 = value / 100;value %= 100;if(d3 == 9) {System.out.print("CM");} else if(d3 == 4) {System.out.print("CD");} else {if(d3 >= 5) {System.out.print("D");d3 -= 5;}for(int i=0; i<d3; i++) {System.out.print("C");}}int d2 = value / 10;value %= 10;if(d2 == 9) {System.out.print("XC");} else if(d2 == 4) {System.out.print("XL");} else {if(d2 >= 5) {System.out.print("L");d2 -= 5;}for(int i=0; i<d2; i++) {System.out.print("X");}}int d1 = value;if(d1 == 9) {System.out.print("IX");} else if(d1 == 4) {System.out.print("IV");} else {if(d1 >= 5) {System.out.print("V");d1 -= 5;}for(int i=0; i<d1; i++) {System.out.print("I");}}System.out.println();}static class FastScanner {private final InputStream in = System.in;private final byte[] buffer = new byte[1024];private int ptr = 0;private int buflen = 0;private boolean hasNextByte() {if (ptr < buflen) {return true;}else{ptr = 0;try {buflen = in.read(buffer);} catch (IOException e) {e.printStackTrace();}if (buflen <= 0) {return false;}}return true;}private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}private void skipUnprintable() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;}public boolean hasNext() { skipUnprintable(); return hasNextByte();}public String next() {if (!hasNext()) throw new NoSuchElementException();StringBuilder sb = new StringBuilder();int b = readByte();while(isPrintableChar(b)) {sb.appendCodePoint(b);b = readByte();}return sb.toString();}public long nextLong() {return Long.parseLong(next());}public int nextInt(){return Integer.parseInt(next());}}}