嗨我試圖寫一個Java代碼的問題陳述:的StackOverflowError在尋找無可
找到沒有分發ň硬幣M個成員的方式,其中一人是隊長(分銷商)。每個會員可以一次拿1個硬幣,並將其傳遞給其他成員,包括隊長。船長不能擁有第一枚硬幣。但是,當只剩下一枚硬幣時,應該給上尉一枚硬幣。這可能有多少種方式?
我綁了這個。但得到StackOverflowError
。請幫忙。
我在這裏開始做呼叫作爲解決(1,N)
private static int solve(int r, int n)
{
int count = 0;
if(n==2 && r!=1)
{
return 1;
}
if(n==2 && r==1)
{
return 2;
}
for(int i=1;i<=m;i++)
{
if(r!=i)
count += solve(i,--n);
}
return count;
}
堆棧跟蹤
Exception in thread "main" java.lang.StackOverflowError
at NoPrey.solve(NoPrey.java:50)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:62)
at NoPrey.solve(NoPrey.java:
想這是你的功課,你想,而你花時間在Facebook/Twitter要完成它。 –
'for(int i = 1; i <= m; i ++)'你的方法中m是多少?你得到錯誤的r和n的值是多少? – YoungHobbit
@Balwinder那不是我的功課先生,我正在學習java,並面臨一些問題.. @ YoungHobbit m是不。的成員 – Ashwani