結果

問題 No.10 +か×か
ユーザー masamasa
提出日時 2015-01-19 14:25:39
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 777 bytes
コンパイル時間 426 ms
コンパイル使用メモリ 63,528 KB
実行使用メモリ 7,600 KB
最終ジャッジ日時 2023-09-04 21:37:56
合計ジャッジ時間 7,159 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 6 ms
4,380 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using namespace std;

int n, total;
vector<int> a;
string ans;

bool calc(int result, int idx) {
	if (idx == n) {
		// cout << ans << " " << result << endl;
		if (result == total) {
			return true;
		}
		return false;
	}

	for (int i = 0; i < 2; i++) {
		int next;
		if (i == 0) {
			ans[idx - 1] = '+';
			next = result + a[idx];
		} else {
			ans[idx - 1] = '*';
			next = result * a[idx];
		}
		if (next <= total) {
			if (calc(next, idx + 1)) {
				return true;
			}
		}
	}
	return false;
}

int main() {
	cin >> n;
	cin >> total;

	a.assign(n, 0);
	ans.assign(n - 1, '?');

	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}

	calc(a[0], 1);
	cout << ans << endl;
	return 0;
}
0