所以現在我正在爲我的最後一篇文章編寫一些代碼,它需要你在system.in的一些輸入中紅色,處理它,第一行總是一個一組數字高達25個數字。接下來是一個N或L,另一個是目標。使用這個輸入你必須找到正確的一組操作(+和*),它們使用int值來創建目標。布爾數組和一個簡單的方法來嘗試每個組合
我正在使用一個布爾數組來跟蹤我在非常檢查中使用的操作數,但是我不確定如何通過嘗試每個不同的操作數來「強制」解決方案,我有代碼來檢查每個設置但是我不知道是否有一個簡單和容易的方法來改變數組[0,0,0,0](0爲false)到[0,0,0,1],[0,0, 1,0],[0,0,1,1]等?
我確定有一個非常簡單的方法,我忽略了,但對於我的生活,我不確定它是什麼atm。
static boolean evlN(int[] input, boolean[]ops, int aim){
boolean run = true, found = false;
int[] used = new int[input.length];
int runs = 0 ,ans = 0;
while(!found && runs < (1 << ops.length)){
//finding all multiplys and doing them first
search:
for(int x = 0; x < ops.length; x++){
if(!ops[x]){
used[x] = input[x] * input[x+1];
//need to stop working out and change the ops
if(used[x] > aim){
run = false;
break;
}
}
}
//once multiplys have been done need to do all the adds
if(run){
for(int x = 0; x < ops.length; x++){
if(ops[x]){
if(used[x] != 0) ans += used[x] + input[x+1];
else if(used[x+1] != 0) ans += input[x] + used[x];
}
if(ans > aim) break;
}
}
if(ans == aim) found = true;
used = new int[input.length];
ans= 0;
runs++;
run = !run;
}
if(found) return true;
else return false;
}
這是即時通訊使用的是什麼工作了每一組操作數和數字,我只是試圖改變布爾數組蠻力答案
'我有代碼來檢查每個集合... ...你能告訴我們這個代碼嗎? –
您可以使用一個位集而不是一個布爾數組,然後遞增。 – shmosel
@shmosel你如何使用一個位集?我知道你可以做1 << bitset或者我錯了,我不確定,因爲我從來沒有和他們合作過 – NexMetu