結果

問題 No.10 +か×か
コンテスト
ユーザー mudbdb
提出日時 2015-06-22 22:38:39
言語 C90(gcc12)
(gcc 12.4.0)
コンパイル:
gcc-12 -O2 -std=c90 -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,903 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 171 ms
コンパイル使用メモリ 32,404 KB
最終ジャッジ日時 2026-02-23 18:35:31
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12 WA * 1
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.c: In function ‘main’:
main.c:95:3: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   95 |   scanf("%d", &N);
      |   ^~~~~~~~~~~~~~~
main.c:96:3: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   96 |   scanf("%d", &Total);
      |   ^~~~~~~~~~~~~~~~~~~
main.c:98:23: warning: ignoring return value of ‘scanf’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   98 |   for (i=0; i<N; i++) scanf("%d", &(A[i]));
      |                       ^~~~~~~~~~~~~~~~~~~~

ソースコード

diff #
raw source code

#include <stdio.h>
#include <stdlib.h>
int N;
int Total;
int *A = 0;
char *Op = 0;
int max_chk(int i, int total)
{
  int rc;
  if (total <= Total) {
    if (A[i+1] == 1) {
      total += A[i+1];
    } else {
      total *= A[i+1];
    }
    if (i < N-2) {
      rc = max_chk(i+1, total);
    } else {
      rc = total;
    }
  } else {
    rc = total;
  }
  return rc;
}
int add(int i, int total)
{
  int rc = 0;
  if (i < N-1) {
    total += A[i+1];
    Op[i] = '+';
    Op[i+1] = '\0';
    if (total == Total) {
      if (i == N-2) {
        rc = 1;
      } else if (1 == mul(i+1, total)) {
        rc = 1;
      } else {
        rc = 0;
      }
    } else if (total < Total) {
      if (Total <= max_chk(i+1, total)) {
        if (1 == add(i+1, total)) {
          rc = 1;
        } else if (1 == mul(i+1, total)) {
          rc = 1;
        } else {
          rc = 0;
        }
      } else {
        rc = 0;
      }
    } else {
      rc = 0;
    }
  }
  return rc;
}
int mul(int i, int total)
{
  int rc = 0;
  if (i < N) {
    total *= A[i+1];
    Op[i] = '*';
    Op[i+1] = '\0';
    if (total == Total) {
      if (i == N-2) {
        rc = 1;
      } else if (1 == mul(i+1, total)) {
        rc = 1;
      } else {
        rc = 0;
      }
    } else if (total < Total) {
      if (Total <= max_chk(i+1, total)) {
        if (1 == add(i+1, total)) {
          rc = 1;
        } else if (1 == mul(i+1, total)) {
          rc = 1;
        } else {
          rc = 0;
        }
      } else {
        rc = 0;
      }
    } else {
      rc = 0;
    }
  }
  return rc;
}
int main()
{
  int i;
  scanf("%d", &N);
  scanf("%d", &Total);
  A = (int*)malloc(sizeof(int)*N);
  for (i=0; i<N; i++) scanf("%d", &(A[i]));
  Op = (char*)malloc(sizeof(char)*N);

  if (1 == add(0, A[0])) {
  } else if (1 == mul(0, A[0])) {
  } else {
    Op[0] = '!';
    Op[1] = '\0';
  }

  printf("%s\n", Op);
  return 0;
}
0