我知道问题,但是不会改

P1150 Peter 的烟

VegetableCode @ 2021-07-24 22:21:15

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

这里应该:
判断条件应该是 n + itme >= k
语句 n = n / k; 改为 n = (n + item) / k;
但是会因为第一次计算错误而导致结果错误,有没有大佬可以帮忙改一下


by VegetableCode @ 2021-07-24 22:36:53

最笨的办法是分离第一次

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

by VegetableCode @ 2021-07-24 22:40:24

但是我觉得好冗杂,看着这个代码(手动狗头)


by 迷失再黑夜里 @ 2021-07-24 22:44:41

@VegetableCode

You can try this one.

#include<bits/stdc++.h>
int n,k,jsq=0;
using namespace std;
int main()
{
    cin>>n>>k;
    jsq=(n-2+1)/(k+1-2)+n;
    cout<<jsq<<endl;
    return 0;
}

OK?


|