我试图解决项目euler 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
使用任意数量的硬币可以使用多少种不同的方式?
使用此代码:
#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,但我不知道为什么,有人可以帮助我吗?
I tried to solve project euler 31:
In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p). It is possible to make £2 in the following way:
1×£1 + 1×50p + 2×20p + 1×5p + 1×2p + 3×1p
How many different ways can £2 be made using any number of coins?
with this code:
#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; }But the code gives 73685, not 73682 but I don't know why, could someone help me plz?
最满意答案
为什么将acc初始化为1? (当x是函数数的倍数时才有意义,但只有那时。)将其更改为0,并将循环条件更改为x>=0 。 (如果我理解你的代码)。
Why do you initialize acc to 1? (It makes sense when x is a multiple of the number of the function, but only then.) change it to 0, and change the loop condition to x>=0. (If I understood your code).
更多推荐
发布评论