結果

問題 No.297 カードの数式
ユーザー masamasa
提出日時 2015-11-06 23:22:35
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,180 bytes
コンパイル時間 637 ms
コンパイル使用メモリ 69,872 KB
実行使用メモリ 5,292 KB
最終ジャッジ日時 2023-10-11 14:56:53
合計ジャッジ時間 1,718 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 WA -
testcase_07 AC 2 ms
4,352 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,352 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 2 ms
4,352 KB
testcase_13 WA -
testcase_14 AC 1 ms
4,348 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 2 ms
4,352 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 AC 1 ms
4,348 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:74:7: warning: ‘ans2’ may be used uninitialized in this function [-Wmaybe-uninitialized]
  ans2 += tmp;
  ~~~~~^~~~~~

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <string>

using namespace std;

int main() {
	int n;
	char c;

	cin >> n;
	vector<int> nums;
	int plus = 0, minus = 0;

	for (int i = 0; i < n; i++) {
		cin >> c;
		if (c == '+') {
			plus++;
		} else if (c == '-') {
			minus++;
		} else {
			nums.push_back(c - '0');
		}
	}

	int plus2 = plus;
	int minus2 = minus;

	// for max
	long long ans1 = 0;
	sort(nums.begin(), nums.end(), greater<int>());

	int idx = 0;
	while (idx < nums.size() - (plus + minus)) {
		ans1 *= 10;
		ans1 += nums[idx];
		idx++;
	}

	while (plus > 0) {
		ans1 += nums[idx];
		idx++;
		plus--;
	}
	while (minus > 0) {
		ans1 -= nums[idx];
		idx++;
		minus--;
	}

	long long ans2;
	if (minus2 > 0) {
		ans2 = -ans1;
		cout << ans1 << " " << ans2 << endl;
		return 0;
	}

	int group = plus2 + 1;
	sort(nums.begin(), nums.end());

	long long tmp = 0;
	long long digit = 1;
	idx = 0;
	while (idx < nums.size()) {
		for (int i = 0; i < group && i + idx < nums.size(); i++) {
			tmp += nums[i] * digit;
		}
		idx += group;
		digit *= 10;
	}
	ans2 += tmp;

	cout << ans1 << " " << ans2 << endl;
	return 0;
}
0