結果

問題 No.10 +か×か
ユーザー scachescache
提出日時 2014-11-13 07:00:56
言語 Java21
(openjdk 21)
結果
AC  
実行時間 158 ms / 5,000 ms
コード長 1,131 bytes
コンパイル時間 4,363 ms
コンパイル使用メモリ 74,400 KB
実行使用メモリ 60,652 KB
最終ジャッジ日時 2023-08-21 06:08:35
合計ジャッジ時間 5,883 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
59,936 KB
testcase_01 AC 128 ms
59,796 KB
testcase_02 AC 126 ms
60,580 KB
testcase_03 AC 144 ms
60,384 KB
testcase_04 AC 137 ms
59,928 KB
testcase_05 AC 132 ms
60,188 KB
testcase_06 AC 158 ms
60,488 KB
testcase_07 AC 151 ms
60,168 KB
testcase_08 AC 139 ms
60,148 KB
testcase_09 AC 137 ms
60,248 KB
testcase_10 AC 128 ms
60,652 KB
testcase_11 AC 126 ms
59,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Scanner;

public class PlusOrMult_2 {
	public static void main(String[] args) {
		PlusOrMult_2 p = new PlusOrMult_2();
	}

	public PlusOrMult_2() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int total = sc.nextInt();
		int[] a = new int[n];
		for(int i=0;i<n;i++){
			a[i] = sc.nextInt();
		}
		
		solve(n, total, a);
	}

	boolean[][] memo;
	StringBuilder sb;
	public void solve(int n, int total, int[] a) {
		memo = new boolean[50+1][100000+1];
		sb =new StringBuilder();
		
		rec(1, total, a[0], a);
		System.out.println(sb.toString());
	}
	
	private boolean rec(int c, int total, int cur, int[] a){
		if(c >= a.length){
			if(cur==total)
				return true;
			else
				return false;
		}
		
		if(cur > total)
			return false;
		
		if(memo[c][cur])
			return false;
		
		boolean res = rec(c+1, total, cur+a[c], a);
		if(res){
			sb.insert(0, '+');
			return true;
		}
		
		res = rec(c+1, total, cur*a[c], a);
		if(res){
			sb.insert(0, '*');
			return true;
		}
		
		
		memo[c][cur] = true;
		return false;
		
		
	}
	
	
	

}
0