我在一所學校分配工作,我有以下問題:需要幫助傷點在我的Java代碼
我給出了一些枝(用不同的或相似的長度),和我負責找取出形成給定長度的較長棒所需的最小棒數量。
例如, 給定6根長度爲1,1,1,1,1,3的長度爲5的長棒,輸出爲3. 注意:棒不能重複使用。
但是,如果不可能形成給定長度,則輸出-1。 例如, 給定3個長度爲1,2,6的棒,爲了形成長度爲5的較長棒,輸出將是-1。
我有以下代碼,它已通過所有公開測試用例。但是,我沒有通過私人測試案例,我無法弄清楚我的錯誤。
這裏是我的代碼:
import java.util.*;
class Result {
static int min = 100000;
public static int solve(int pos, int currSum, int len, int numStk) {
// implementation
for (int i=1; i<=Stick.data.length - pos; i++){
if (currSum > len){
continue;
}
else if (currSum < len){
if (pos+i >= Stick.data.length){
break;
}
else{
solve(pos+i,currSum+Stick.data[pos+i], len, numStk+1);
}
}
else if (currSum == len){
if (numStk < min){
min = numStk;
}
}
}
return min;
}
}
class Stick {
static int[] data;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sticks = sc.nextInt();
data = new int[sticks];
int len = sc.nextInt();
for (int i=0; i<sticks; i++){
data[i] = sc.nextInt();
}
for (int i=0; i<sticks; i++){
Result.solve(i,0,len,1);
}
if (Result.min == 100000){
System.out.println(-1);
} else {
System.out.println(Result.min-1);
}
}
}
請提供期望的結果,實際結果以及您爲調試代碼所採取的步驟。 – 2014-10-27 15:14:00
什麼是「私人測試用例」? – 2014-10-27 15:14:27
**「但是,我沒有通過私人測試......」** - 那會是什麼,到底是什麼? – azurefrog 2014-10-27 15:14:34