2012-02-06 75 views
-1

我試圖解決project euler 31錯誤導致項目歐拉31

在英格蘭的貨幣是由英鎊,£和便士,P,並 有八個硬幣一般循環:

1p,2p,5p,10p,20p,50p,1英鎊(100p)和2英鎊(200p)。它可以 下列方式使£2:1

1×£+ 1×50P + 2×20P + 1×5P + 1×2P + 3×1P

有多少種方式可以2英鎊是 使用任何數量的硬幣?

與此代碼:

#define to2(x) ((x)/2+1) 

int to5(x) 
{ 
    int acc=1; 
    for(;x>0;x-=5) 
     acc+=to2(x); 
    return acc; 
} 

int to10(x) 
{ 
    int acc=1; 
    for(;x>0;x-=10) 
     acc+=to5(x); 
    return acc; 
} 

int to20(x) 
{ 
    int acc=1; 
    for(;x>0;x-=20) 
     acc+=to10(x); 
    return acc; 
} 

int to50(x) 
{ 
    int acc=1; 
    for(;x>0;x-=50) 
     acc+=to20(x); 
    return acc; 
} 

int to100(x) 
{ 
    int acc=1; 
    for(;x>0;x-=100) 
     acc+=to50(x); 
    return acc; 
} 

int main() 
{ 
    int test = to100(200)+1; 
    printf("%d",test); 
    return 0; 
} 

但代碼給出了73685,而不是73682,但是我不知道爲什麼,可能有人幫我PLZ?

+1

它應該計算什麼?這個問題應該是獨立的,而不僅僅是在看過其他網站的作業之後。 – jpalecek 2012-02-06 15:30:11

+3

如果可以的話,不是每個人都知道歐拉項目31.你想要解決什麼問題? – Bart 2012-02-06 15:31:09

回答

6

爲什麼初始化acc爲1? (x是函數編號的倍數,但只有這樣纔有意義。)將其更改爲0,並將環路條件更改爲x>=0。 (如果我理解你的代碼)。

+0

非常感謝,現在有意義。 – chubakueno 2012-02-06 15:37:28

+0

@chubakueno一定要將[answer as mark]標記爲(http://stackoverflow.com/faq#howtoask) – 2012-02-06 15:52:06

1

這個問題不需要C代碼來解決,這是一個設置選擇的問題。你需要解決number of ways to select n coins where n > 0 and sum = 2£,你應該查找Set theory
這應該可以幫助您找到線性方程來計算數字。

+0

甚至比集合論更好,找[Enumerative Combinatorics](http://en.wikipedia.org/ wiki/Enumeration)和[Generating Functions](http://en.wikipedia.org/wiki/Generating_function) – 2012-02-06 23:48:24