为什么这种求位数的代码得不到正确答案

P1045 [NOIP2003 普及组] 麦森数

1004779564bzbjj @ 2023-10-02 11:46:43

#include<bits/stdc++.h>
using namespace std;
long long ed[51],p,ans,t=2;
int main(){
    cin>>p;
    for(int i=1;i<=p;i++){
        t*=2;
        if(t>=100000){
            t/=10;
            ans++;
        }
        //if(i%1000==0)
        //printf("%d ",t);
        //cout<<t<<" ";
        //for(int j=1;j<=ans;j++)cout<<"0";
        //cout<<endl;
    }
    cout<<ans+5<<endl;
    printf("%d\n",(int)(log10(2)*p+1));
    return 0;
}

in:114514 out:34473 34473

in:233333 out:70240 70241

in:666666 out:200685 200687

in:1919810 out:577914 577921


by Aventurine_stone @ 2023-12-21 16:16:24

0.30102999566*p+1 会更好


|