結果

問題 No.10 +か×か
ユーザー scache
提出日時 2014-11-13 07:00:56
言語 Java
(openjdk 23)
結果
AC  
実行時間 173 ms / 5,000 ms
コード長 1,131 bytes
コンパイル時間 3,786 ms
コンパイル使用メモリ 77,364 KB
実行使用メモリ 48,084 KB
最終ジャッジ日時 2024-12-14 15:25:52
合計ジャッジ時間 6,360 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

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