結果

問題 No.297 カードの数式
ユーザー GrenacheGrenache
提出日時 2015-11-07 00:32:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 121 ms / 1,000 ms
コード長 2,268 bytes
コンパイル時間 3,183 ms
コンパイル使用メモリ 74,636 KB
実行使用メモリ 56,268 KB
最終ジャッジ日時 2023-08-26 22:49:15
合計ジャッジ時間 7,346 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 117 ms
55,352 KB
testcase_01 AC 118 ms
55,912 KB
testcase_02 AC 117 ms
55,612 KB
testcase_03 AC 119 ms
55,784 KB
testcase_04 AC 117 ms
56,116 KB
testcase_05 AC 116 ms
56,200 KB
testcase_06 AC 116 ms
55,956 KB
testcase_07 AC 117 ms
56,216 KB
testcase_08 AC 116 ms
55,816 KB
testcase_09 AC 118 ms
55,988 KB
testcase_10 AC 117 ms
55,756 KB
testcase_11 AC 117 ms
55,688 KB
testcase_12 AC 116 ms
55,904 KB
testcase_13 AC 117 ms
55,872 KB
testcase_14 AC 117 ms
55,984 KB
testcase_15 AC 119 ms
55,976 KB
testcase_16 AC 117 ms
55,956 KB
testcase_17 AC 116 ms
55,652 KB
testcase_18 AC 117 ms
55,872 KB
testcase_19 AC 116 ms
55,972 KB
testcase_20 AC 118 ms
56,268 KB
testcase_21 AC 119 ms
56,048 KB
testcase_22 AC 118 ms
56,012 KB
testcase_23 AC 117 ms
56,096 KB
testcase_24 AC 118 ms
55,760 KB
testcase_25 AC 121 ms
55,996 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;


public class Main_yukicoder297 {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        char[] c = new char[n];
        int p = 0;
        int m = 0;
        int[] d = new int[10];
        for (int i = 0; i < n; i++) {
        	c[i] = sc.next().charAt(0);
        	if (c[i] == '+') {
        		p++;
        	} else if (c[i] == '-') {
        		m++;
        	} else {
        		d[c[i] - '0']++;
        	}
        }
        
        long[] num = new long[p + m + 1];
        for (int i = 0; i < p + m + 1 - 1; i++) {
        	for (int j = 0; j < 10; j++) {
        		if (d[j] > 0) {
        			num[i] = j;
        			d[j]--;
        			break;
        		}
        	}
        }

        long tmp = 0;
        for (int i = 9; i >= 0; i--) {
        	while (d[i] > 0) {
        		tmp = tmp * 10 + i;
        		d[i]--;
        	}
        }
        num[p + m + 1 - 1] = tmp;

        long max = 0;
        int tmpp = p;
        int tmpm = m;
        for (int i = p + m; i >= 0; i--) {
        	if (i == p + m) {
        		max += num[i];
        	} else if (tmpp > 0) {
        		max += num[i];
        		tmpp--;
        	} else if (tmpm > 0) {
        		max -= num[i];
        		tmpm--;
        	}
        }

        long min = 0;
        tmpp = p;
        tmpm = m;
        for (int i = 0; i < p + m + 1; i++) {
        	if (i == 0) {
        		min += num[i];
        	} else if (tmpp > 0) {
        		min += num[i];
        		tmpp--;
        	} else if (tmpm > 0) {
        		min -= num[i];
        		tmpm--;
        	}
        }

        if (m == 0) {
        	min = 0;
            for (int i = 0; i < n; i++) {
            	if (c[i] == '+') {
            	} else if (c[i] == '-') {
            	} else {
            		d[c[i] - '0']++;
            	}
            }

            long base = 1;
            int i = 0;
        	for (int j = 9; j >= 0; j--) {
        		while (d[j] > 0) {
        			min += base * j;
        			d[j]--;
            		i++;
            		if (i > p + m) {
            			i = 0;
            			base *= 10;
            		}
        		}
            }
        }
        
        System.out.printf("%d %d\n", max, min);

        sc.close();
    }
}
0