C 20分不太明白错哪了,求解答一下

P1150 Peter 的烟

XianYuzhinan @ 2024-05-08 18:41:13

#include<stdio.h>
int main()
{
    int n, k, a = 0, sum = 0;
    scanf("%d%d", &n, &k);
    while (n > 0)
    {
        --n;
        ++a;
        if (a == k)
        {
            a -= 3;
            n += 1;
        }
        sum++;
    }
    printf("%d", sum);

    return 0;
}

by kongyanxiang @ 2024-05-08 19:14:40

@XianYuzhinan 判断里面你减3干什么??


by kongyanxiang @ 2024-05-08 19:15:30

@XianYuzhinan 改成-=k


by LHM_zs @ 2024-05-08 19:46:22

将12行的a-=3改成a=0就可以了

#include<stdio.h>
int main()
{
    int n,k,a=0,sum=0;
    scanf("%d%d",&n,&k);
    while(n>0)
    {
        --n;
        ++a;
        if(a==k)
        {
            a=0;//改这里
            n++;
        }
        sum++;
    }
    printf("%d", sum);
    return 0;
}

|