错误的结果是项目euler 31(Wrong result in a project euler 31)

编程入门 行业动态 更新时间:2024-10-07 08:31:57
错误的结果是项目euler 31(Wrong result in a project euler 31)

我试图解决项目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).

更多推荐

本文发布于:2023-07-19 23:03:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1188583.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   项目   结果是   euler   project

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!