結果

問題 No.297 カードの数式
ユーザー naimonon77
提出日時 2016-01-08 17:36:01
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 1,392 bytes
コンパイル時間 1,668 ms
コンパイル使用メモリ 160,492 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-09-19 13:09:22
合計ジャッジ時間 2,205 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2 WA * 1
other AC * 12 WA * 11
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:67:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   67 |         scanf(" %c",&c);
      |         ~~~~~^~~~~~~~~~

ソースコード

diff #

#include <bits/stdc++.h>
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) REP(i,0,n)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
/* ここからが本編 */
/*  */
char num[18] = {0};
int N;
int num_top = 0;
ll s_max(int plus_cnt,int minus_cnt)
{
  char s_tmp[18];
  int tmp = N-2*plus_cnt-2*minus_cnt;
  strncpy(s_tmp,num,tmp);
  s_tmp[tmp] = 0;
  ll max = atoi(s_tmp);
  while(plus_cnt) {
	max += num[tmp++] - '0';
	plus_cnt--;
  }
  while(minus_cnt) {
	max -= num[tmp++] - '0';
	minus_cnt--;
  }
  return max;
}
ll s_min(int plus_cnt,int minus_cnt)
{
  char s_tmp[18];
  ll min;
  if(minus_cnt) {
	min = num[num_top-1] - '0';
	int tmp = N-2*plus_cnt-2*minus_cnt;
	minus_cnt--;
	strncpy(s_tmp,num,tmp);
	s_tmp[tmp] = 0;
	min -= atoi(s_tmp);
	
	while(minus_cnt) {
	  min -= num[tmp++] - '0';
	  minus_cnt--;
	}
	while(plus_cnt) {
	  min += num[tmp++] - '0';
	  plus_cnt--;
	}
  }
  else {
	return -1;
  }
  return min;
}
int main(void)
{
  int i,j,k;
  int plus_cnt = 0;
  int minus_cnt = 0;
  char c;
  
  cin >> N;
  for(i=0;i<N;i++) {
	scanf(" %c",&c);
	if(c == '+') plus_cnt++;
	else if(c == '-') minus_cnt++;
	else {
	  num[num_top] = c;
	  num_top++;
	}
  }
  sort(num,num+num_top,greater<int>());
  ll max = s_max(plus_cnt,minus_cnt);
  ll min = s_min(plus_cnt,minus_cnt);
  printf("%lld %lld\n",max,min);
  
  return 0;
}
0