結果

問題 No.297 カードの数式
ユーザー ぴろずぴろず
提出日時 2015-11-06 22:52:42
言語 Java21
(openjdk 21)
結果
AC  
実行時間 146 ms / 1,000 ms
コード長 1,188 bytes
コンパイル時間 2,759 ms
コンパイル使用メモリ 86,076 KB
実行使用メモリ 58,796 KB
最終ジャッジ日時 2023-08-26 22:42:42
合計ジャッジ時間 7,593 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 144 ms
56,984 KB
testcase_01 AC 142 ms
56,588 KB
testcase_02 AC 141 ms
56,380 KB
testcase_03 AC 143 ms
56,396 KB
testcase_04 AC 142 ms
56,868 KB
testcase_05 AC 140 ms
56,652 KB
testcase_06 AC 144 ms
56,392 KB
testcase_07 AC 145 ms
57,124 KB
testcase_08 AC 145 ms
56,700 KB
testcase_09 AC 144 ms
56,720 KB
testcase_10 AC 143 ms
56,660 KB
testcase_11 AC 144 ms
56,780 KB
testcase_12 AC 146 ms
56,476 KB
testcase_13 AC 143 ms
56,748 KB
testcase_14 AC 143 ms
56,400 KB
testcase_15 AC 145 ms
56,744 KB
testcase_16 AC 143 ms
56,624 KB
testcase_17 AC 144 ms
56,660 KB
testcase_18 AC 145 ms
56,728 KB
testcase_19 AC 144 ms
56,740 KB
testcase_20 AC 144 ms
56,532 KB
testcase_21 AC 145 ms
56,748 KB
testcase_22 AC 145 ms
58,796 KB
testcase_23 AC 143 ms
56,720 KB
testcase_24 AC 144 ms
56,932 KB
testcase_25 AC 143 ms
56,648 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package no297;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		ArrayList<Integer> d = new ArrayList<>();
		int plus = 0;
		int minus = 0;
		for(int i=0;i<n;i++) {
			char c = sc.next().charAt(0);
			if (Character.isDigit(c)) {
				d.add(c - '0');
			}else if(c == '+') {
				plus++;
			}else{
				minus++;
			}
		}
		Collections.sort(d);
		long max = 0;

		long big = 0;
		for(int i=0;i<n-(plus+minus)*2;i++) {
			big = (big * 10) + d.get(d.size()-1-i);
		}

		max += big;
		for(int i=0;i<minus;i++) {
			max -= d.get(i);
		}
		for(int i=minus;i<minus+plus;i++) {
			max += d.get(i);
		}

		long min = 0;
		if (minus == 0) {
			int i=0;
			int x = 1;
			while(i < d.size()) {
				for(int j=0;j<plus+1;j++) {
					min += x * d.get(d.size()-1-i);
					i++;
					if (i >= d.size()) {
						break;
					}
				}
				x *= 10;
			}
		}else{
			min = -big;
			for(int i=0;i<plus+1;i++) {
				min += d.get(i);
			}
			for(int i=plus+1;i<minus+plus;i++) {
				min -= d.get(i);
			}
		}

		System.out.println(max + " " + min);
	}

}
0