求大佬看一下,为什么错了?

P1150 Peter 的烟

tangqizhang @ 2024-09-20 20:39:10

#include<bits/stdc++.h>
using namespace std;
int main(){
    int n,k,s;
    cin>>n>>k;
    for(int i=0;i<=k;i++){
        s++;
        if(s==n){
            k++;
        }
    }
    cout<<s;
    return 0;
}

by tangqizhang @ 2024-09-20 20:43:35

?


by luoyebushiye @ 2024-09-20 20:44:59

@tangqizhang

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

by a11223344 @ 2024-09-20 20:46:28

你似乎把n和k弄反了,s要赋初值

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

by chen_rcc @ 2024-09-20 20:48:25

s应该写在函数外面 不然要赋值为0


by tangqizhang @ 2024-09-20 20:52:52

感谢大佬们的指导!


by wzxujiacheng__ @ 2024-09-26 21:47:22

用我的

#include<bits/stdc++.h>
using namespace std;
unsigned long long n=0,k=0;
unsigned long long read(){
    unsigned long long ret=0,f=1;
        char ch=getchar();
    while(!isdigit(ch)){
              if (ch=='-')f=-f;ch=getchar();
        }
    while(isdigit(ch)){
        ret=(ret<<3)+(ret<<1)+(ch&15);
        ch=getchar();
   }
    return ret*f;
}
int main(){
    n=read(),k=read();
    unsigned long long cnt=n;
    while(n/k>0){cnt+=n/k;int m=n%k;n=n/k+m;}
    cout<<cnt;
    return 0;
}

|