测试点3错误!求助!

P1150 Peter 的烟

ronghexuan @ 2023-07-21 14:11:50

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

by fangyicheng1216 @ 2023-07-21 14:18:08

数据点3:

输入:

10 2

输出:

19

你的输出:

18


by Happy_Doggie @ 2023-07-21 14:21:03

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,k;
    cin >> n >> k;
    int cnt = n;//初始化等于n,先把有的都抽了
    while (n >= k)//不是n > 1
    {
//      if(cnt % k == 0)没看懂
//          n++;
//      n--;
//      cnt++;
        long long int a = n / k;//能换多少烟
        n = n % k + a;//换的烟头不扔啊
        cnt = cnt + a;
    }
    cout << cnt;
    return 0;
} 
/*
  我的代码: 
  #include<bits/stdc++.h>
  #define MX 100
  using namespace std;
  long long int n,k;
  int main(){
  scanf("%lld%lld",&n,&k);
  long long int sum = n;
  while(n >= k)
  {
      long long int a = n / k;
      n = n % k + a;
      sum = sum + a;
      }
  printf("%lld\n",sum);
  return 0;
  }
 */

by fangyicheng1216 @ 2023-07-21 14:21:40

具体原因就是最后 cnt 有可能会刚好能被 k 整除,这样你的代码就少算了一遍,改成这样就好了:

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

by fangyicheng1216 @ 2023-07-21 14:22:10

@ronghexuan


by ronghexuan @ 2023-07-21 14:26:27

@fangyicheng1216 谢谢大佬QWQ


|