为什么循环条件改成n/k>=0提交反馈为错误?

P1150 Peter 的烟

ziyang22 @ 2024-08-11 20:33:35

#include<stdio.h>
int main (void){
    long k,n,count = 0;

    scanf("%ld %ld",&n,&k);

    while(n/k != 0){
        n = n-k+1;
        printf("n = %ld\n",n);
        count += k;
        printf("count = %ld\n",count);
    }
    count += n;

    printf("%ld",count);

    return 0;
}

这是提交上去为100的解,但是逻辑上应该把while里面的条件改成n/k>=0才对呀?


by Elaina_0 @ 2024-08-11 20:55:10

@ziyang22

除到最后 n=0n<0&&(-n<k) 时仍会再加 k 导致多算了几遍。

求关喵~


|