結果

問題 No.297 カードの数式
ユーザー chuka231chuka231
提出日時 2015-11-07 00:20:22
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,610 bytes
コンパイル時間 396 ms
コンパイル使用メモリ 58,872 KB
実行使用メモリ 4,480 KB
最終ジャッジ日時 2023-10-11 14:40:32
合計ジャッジ時間 1,456 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,352 KB
testcase_02 AC 1 ms
4,356 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 WA -
testcase_05 AC 1 ms
4,356 KB
testcase_06 WA -
testcase_07 AC 1 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 1 ms
4,352 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,352 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 1 ms
4,348 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 2 ms
4,352 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

int main()
{
	int n;
	int dig[10];
	int plus=0, minus=0;

	for (int i = 0; i < 10; i++) dig[i] = 0;

	cin >> n;
	for (int i = 0; i < n; i++) {
		char c;
		cin >> c;

		if (c == '+') {
			plus++;
			continue;
		}
		
		if (c == '-') {
			minus++;
			continue;
		}
		int k = c - 48;
		dig[k]++;
	}

	int dig_temp[10];
	int plus_temp = plus, minus_temp = minus;
	for (int i = 0; i < 10; i++) dig_temp[i] = dig[i];

	//----get max----
	int max = 0;

	int k = 0;
	while (minus) {
		if (dig[k]) {
			max -= k; 
			dig[k]--;
			minus--;
		}
		else {
			k++;
		}
	}

	while (plus) {
		if (dig[k]) {
			max += k;
			dig[k]--;
			plus--;
		}
		else {
			k++;
		}
	}

	int sub_max = 0;
	k = 9;
	while (k >= 0) {
		if (dig[k]) {
			sub_max = sub_max *10 + k;
			dig[k]--;
		}
		else {
			k--;
		}
	}

	max += sub_max;

	if (minus_temp == 0) {
		cout << max << " " << max << endl;
		return 0;
	}

	//---get min---
	plus = plus_temp;
	minus = minus_temp;
	for (int i = 0; i < 10; i++) dig[i] = dig_temp[i];

	int min = 0;
	
	k = 0;
	while (plus) {
		if (dig[k]) {
			min += k;
			dig[k]--;
			plus--;
		}
		else {
			k++;
		}
	}

	for (int i = 0; i < 10; i++) {
		if (dig[i]>0) {
			min += i; dig[i]--; break;
		}
	}

	while (minus > 1) {
		if (dig[k]) {
			min -= k;
			dig[k]--;
			minus--;
		}
		else {
			k++;
		}
	}

	int sub_min = 0;
	k = 9;
	while (k >= 0) {
		if (dig[k]) {
			sub_min = sub_min * 10 + k;
			dig[k]--;
		}
		else {
			k--;
		}
	}

	min = min - sub_min;


	// --- end----
	cout << max << " " << min << endl;

	return 0;
}
0