一个坑

P1035 [NOIP2002 普及组] 级数求和

Akira @ 2017-07-22 18:37:41

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    double k,s=0,i;//一定要double,float会炸
    cin>>k;
    for (i = 1;s<=k; i++)
    {
        s+=1/i;
    }
    cout<<i-1;
    return 0;
}

by 编程的弱子 @ 2017-07-23 16:38:31

#include<iostream>
using namespace std;
int main()
{double s,i;
 int n;
 cin>>n;
 s=0;
 i=1.0;
 do 
 {s=s+1.0/i;
  i++;
 }while (s<=n);
 i--;
 cout<<i;
 return 0; 
}
一个简单的do while循环,最后记得i--就行

by codesonic @ 2017-07-27 10:01:50

OI比赛一般都是用double的。。。我这次还开到long double


by Tsukimaru @ 2017-07-27 16:29:38

原来您们都用浮点数做....我整数变量简化弄了一堆代码0A0


|