結果

問題 No.297 カードの数式
ユーザー htensaihtensai
提出日時 2019-12-26 17:58:14
言語 Java21
(openjdk 21)
結果
AC  
実行時間 148 ms / 1,000 ms
コード長 1,904 bytes
コンパイル時間 2,180 ms
コンパイル使用メモリ 83,744 KB
実行使用メモリ 55,256 KB
最終ジャッジ日時 2024-04-15 04:15:10
合計ジャッジ時間 6,713 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 143 ms
54,868 KB
testcase_01 AC 125 ms
54,660 KB
testcase_02 AC 140 ms
54,848 KB
testcase_03 AC 131 ms
54,540 KB
testcase_04 AC 140 ms
55,024 KB
testcase_05 AC 146 ms
55,008 KB
testcase_06 AC 147 ms
54,992 KB
testcase_07 AC 140 ms
54,860 KB
testcase_08 AC 129 ms
54,640 KB
testcase_09 AC 128 ms
54,588 KB
testcase_10 AC 136 ms
54,896 KB
testcase_11 AC 140 ms
54,696 KB
testcase_12 AC 126 ms
54,500 KB
testcase_13 AC 143 ms
55,256 KB
testcase_14 AC 138 ms
55,040 KB
testcase_15 AC 138 ms
55,152 KB
testcase_16 AC 146 ms
54,744 KB
testcase_17 AC 134 ms
54,576 KB
testcase_18 AC 148 ms
55,032 KB
testcase_19 AC 128 ms
54,720 KB
testcase_20 AC 131 ms
54,544 KB
testcase_21 AC 143 ms
54,888 KB
testcase_22 AC 141 ms
54,932 KB
testcase_23 AC 139 ms
54,964 KB
testcase_24 AC 140 ms
54,884 KB
testcase_25 AC 139 ms
54,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        ArrayList<Integer> numbers = new ArrayList<>();
        ArrayList<Character> chars = new ArrayList<>();
        char[] arr = sc.nextLine().toCharArray();
        for (int i = 0; i < arr.length; i += 2) {
            if (arr[i] >= '0' && arr[i] <= '9') {
                numbers.add(arr[i] - '0');
            } else {
                chars.add(arr[i]);
            }
        }
        Collections.sort(numbers);
        Collections.sort(chars);
        long max = 0;
        int count = numbers.size() - chars.size();
        for (int i = 0; i < count; i++) {
            max *= 10;
            max += numbers.get(numbers.size() - 1 - i);
        }
        long min;
        if (chars.get(chars.size() - 1) == '-') {
            min = - max;
            for (int i = chars.size() - 2; i >= 0; i--) {
                if (chars.get(i) == '+') {
                    min += numbers.get(i + 1);
                } else {
                    min -= numbers.get(i + 1);
                }
            }
            min += numbers.get(0);
        } else {
            int[] values = new int[chars.size() + 1];
            for (int i = 0; i < numbers.size(); i++) {
                int idx = i % values.length;
                values[idx] *= 10;
                values[idx] += numbers.get(i);
            }
            min = 0;
            for (int i = 0; i < values.length; i++) {
                min += values[i];
            }
        }
        for (int i = 0; i < chars.size(); i++) {
            if (chars.get(chars.size() - 1 - i) == '+') {
                max += numbers.get(i);
            } else {
                max -= numbers.get(i);
            }
        }
        System.out.println(max + " " + min);
    }
    
}
0