Help!

P1035 [NOIP2002 普及组] 级数求和

zyx1127 @ 2024-02-16 20:31:13

我不理解,为什么在洛谷能过,一本通不行

  1. #include<bits/stdc++.h>
    using namespace std;
    int main(){    
    int K,n=0;
    double sn=0;
    scanf ("%d",&K);
    while(sn<K){
        n++;
        sn+=(float)1/n;
        }
    printf ("%d",n);
    return 0;
    }

by zyx1127 @ 2024-02-16 20:32:20

include<bits/stdc++.h>

using namespace std; int main(){
int K,n=0; double sn=0; scanf ("%d",&K); while(sn<K){ n++; sn+=(float)1/n; } printf ("%d",n); return 0; }```

include<bits/stdc++.h>

using namespace std; int main(){
int K,n=0; double sn=0; scanf ("%d",&K); while(sn<K){ n++; sn+=(float)1/n; } printf ("%d",n); return 0; }


by HEROBRINEH @ 2024-02-16 20:47:12

#include<bits/stdc++.h> 
using namespace std; 
int main(){
    int K,n=0;
    double sn=0; 
    scanf ("%d",&K); 
    while(sn<K){ 
            n++; 
            sn+=(float)1/n; 
    } 
    printf ("%d",n); 
    return 0; 
}

稍整理,看看Markdown手册吧.


by HEROBRINEH @ 2024-02-16 20:51:20

#include<iostream>
using namespace std;
int main() {
    int k,n=0;
    double a=0;
    cin>>k;
    while(a<=k) {
        n++;    
        a=a+(double)1/n;
    }
    cout<<n;
    return 0;
}

我是这么写的,你的

sn+=(float)1/n;

貌似要把float改成double,具体是不是我也不知道


|