結果

問題 No.297 カードの数式
ユーザー chuka231chuka231
提出日時 2015-11-07 00:04:44
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,632 bytes
コンパイル時間 413 ms
コンパイル使用メモリ 59,084 KB
実行使用メモリ 5,232 KB
最終ジャッジ日時 2023-10-11 14:34:31
合計ジャッジ時間 1,516 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 1 ms
4,348 KB
testcase_04 WA -
testcase_05 AC 1 ms
4,348 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
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;

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

	while (minus) {
		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