错误的结果是项目euler 31

chu*_*eno -1 c c++

我试图解决项目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;
}
Run Code Online (Sandbox Code Playgroud)

但代码给出73685,而不是73682,但我不知道为什么,有人可以帮我PLZ吗?

asa*_*elr 6

为什么要初始化acc为1?(当x函数数量的倍数,但只有那时才有意义.)将其更改为0,并将循环条件更改为x>=0.(如果我理解你的代码).