結果

問題 No.297 カードの数式
ユーザー nksk38nksk38
提出日時 2017-07-29 17:06:56
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,652 bytes
コンパイル時間 681 ms
コンパイル使用メモリ 85,636 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-19 02:47:00
合計ジャッジ時間 1,585 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<functional>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<numeric>
#include<limits>

#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()

using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef pair<ll, string> pls;

int s[50];

int main() 
{
	int N,pl=0,min=0;
	cin >> N;

	int j = 0,k =0;
	for (int i = 0; i < N; i++) {
		char c; cin >> c;
		if (c >= '0' && c <= '9')
			s[j++] = c-'0';
		else {
			if (c == '+')pl++;
			else min++;
		}
			
	}

	sort(s, s + j);
	reverse(s+1,s+j);

	ll mi = s[0], sum = pl + min,sum1;
	int min_t = min, pl_t = pl;
	sum1 = sum;
	string num1 = "";
	char ope;
	
	for (int i = 1; i < j; i++) {
		if (sum != 0) {
			if (min != 0) {
				ope = '-';
				min--;
			}
			else if (pl != 0) {
				ope = '+';
				pl--;
			}
			sum--;
		}
		num1 += to_string(s[i]);	

		if (sum != 0) {
			if (ope == '-') {
				mi = mi - stoll(num1);
			}
			else if (ope == '+')
				mi = mi + stoll(num1);
			num1 = ' ';
		}
	}
	
	if (ope == '-')mi = mi - stoll(num1);
	else if (ope == '+')mi = mi + stoll(num1);

	sort(s, s + j);
	reverse(s, s+j);

	ll mx = 0;
	num1 = ' ';
	ope = '+';
	for (int i = 0; i < j; i++) {
		if (j - (i + 1) <sum1) {
			if (pl_t != 0) {
				ope = '+';
				pl_t--;
			}
			else {
				ope = '-';
			}
		}
		num1 += to_string(s[i]);
		if (j - (i + 1) <= sum1) {
			if (ope == '-')
				mx = mx - stoll(num1);
			else
				mx = mx + stoll(num1);
			num1 = ' ';
		}
	}
	cout << mx << " " << mi << endl;
	return	0;
}
0