結果

問題 No.518 ローマ数字の和
ユーザー Shun_PIShun_PI
提出日時 2017-05-28 22:20:15
言語 Java21
(openjdk 21)
結果
AC  
実行時間 57 ms / 2,000 ms
コード長 4,458 bytes
コンパイル時間 5,053 ms
コンパイル使用メモリ 78,480 KB
実行使用メモリ 53,296 KB
最終ジャッジ日時 2023-10-21 14:20:05
合計ジャッジ時間 4,691 ms
ジャッジサーバーID
(参考情報)
judge9 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
53,284 KB
testcase_01 AC 54 ms
51,292 KB
testcase_02 AC 55 ms
53,288 KB
testcase_03 AC 56 ms
53,292 KB
testcase_04 AC 55 ms
53,296 KB
testcase_05 AC 54 ms
53,296 KB
testcase_06 AC 57 ms
53,292 KB
testcase_07 AC 57 ms
52,176 KB
testcase_08 AC 56 ms
53,292 KB
testcase_09 AC 55 ms
53,296 KB
testcase_10 AC 55 ms
53,284 KB
testcase_11 AC 55 ms
52,164 KB
testcase_12 AC 55 ms
53,288 KB
testcase_13 AC 55 ms
53,288 KB
testcase_14 AC 57 ms
53,280 KB
testcase_15 AC 55 ms
53,272 KB
testcase_16 AC 56 ms
53,292 KB
testcase_17 AC 56 ms
53,288 KB
testcase_18 AC 56 ms
52,200 KB
testcase_19 AC 55 ms
52,176 KB
testcase_20 AC 55 ms
53,292 KB
testcase_21 AC 55 ms
53,288 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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());
	    }
	}
}
0