結果

問題 No.10 +か×か
ユーザー alpha_virginis
提出日時 2015-03-07 00:13:11
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
TLE  
実行時間 -
コード長 1,075 bytes
コンパイル時間 735 ms
コンパイル使用メモリ 72,936 KB
実行使用メモリ 10,144 KB
最終ジャッジ日時 2024-06-24 14:51:18
合計ジャッジ時間 7,372 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 4 TLE * 1 -- * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
#include <math.h>

#include <iostream>
#include <vector>
#include <list>
#include <algorithm>

long sub_f1(long total, long value, long depth, long limit_depth, std::vector<long>& v, std::vector<char>& ans) {
  long temp;

  if( depth == limit_depth ) {
    return value;
  }
  
  temp = value + v[depth];
  if( temp <= total ) {
    temp = sub_f1(total, temp, depth+1, limit_depth, v, ans);
    if( temp == total ) {
      ans.push_back('+');
      return temp;
    }
  }

  temp = value * v[depth];
  if( temp <= total ) {
    temp = sub_f1(total, temp, depth+1, limit_depth, v, ans);
    if( temp == total ) {
      ans.push_back('*');
      return temp;
    }
  }

  return -1;
}

int main() {

  long i, j;
  long n, m;

  long total;

  std::vector<long> v;
  std::vector<char> ans;

  long temp;

  std::cin >> n >> total;
  for(i = 0; i < n; i++) {
    std::cin >> temp;
    v.push_back(temp);
  }

  sub_f1(total, v[0], 1, n, v, ans);

  for(i = ans.size()-1; i >= 0; i--) {
    std::cout << ans[i];
  }

  std::cout << std::endl;

  return 0;
}

0