全WA了

P1035 [NOIP2002 普及组] 级数求和

Hzk119 @ 2024-08-16 11:17:43

#include<iostream> 
using namespace std; 

int main()
{
    int n,k,ans=0;
    cin>>k;
    for(int i=1;i<=k;++i)
    {

    n+=1.0/i; 
    if(n>k*1.0)
        cout<<n;
        break;
    }

    return 0;
} 

by liuye20100123 @ 2024-08-16 11:24:55

n开double


by Emil_ @ 2024-08-16 11:27:24

@Hzk119

#include <bits/stdc++.h>
using namespace std;
double s,k,i=1.0;
int main(){
    cin>>k;
    while(s<=k){
        s+=1.0/i;
        i++;
    }
    cout<<i-1;//最后多加了一次,所以-1 
    return 0;
}

求关


by Hzk119 @ 2024-08-16 11:29:59

谢谢,已关


by cjhchenjinghao @ 2024-08-25 13:55:32

@Hzk119 用双精度浮点


|