为什么总缺1?

P1035 [NOIP2002 普及组] 级数求和

qiaozichen2008 @ 2019-08-06 22:25:46

include<iostream>

using namespace std; int main() { int n,k,i; double s; cin>>k; for(i=1;i<n;i++) { s+=1/i; if(s>k) break; } cout<<i+1; return 0; }


by turTLE_Null @ 2019-08-06 22:26:58

希望更丰富的展现?使用Markdown


by HYSK_残雪 @ 2019-08-06 22:27:08

望更展?用MD


by fzfnf @ 2019-08-06 22:27:54

初始化好变量应该就对了


by HYSK_残雪 @ 2019-08-06 22:28:39

你的n是干哈的


by fzfnf @ 2019-08-06 22:28:43

可以用调和级数公式做


by HYSK_残雪 @ 2019-08-06 22:31:05

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

by lemir3 @ 2019-08-06 22:36:45

加个1上去不就好了吗(抖机灵)


by WRN041012 @ 2019-08-16 13:28:33

include<stdio.h>

int main() { int k,n=0; scanf("%d",&k); for(double Sn=0;Sn<=k;++n,Sn+=1.0/n); printf("%d",n); return 0; }


|