救救孩子

P1035 [NOIP2002 普及组] 级数求和

LingYun8 @ 2019-11-09 21:25:09


    #include<bits/stdc++.h>
    #include<iostream>
    using namespace std;
    int main()
    {
        float b=1,q,i=2;
        int K;
        cin>>K;
        for (i;b<=K; i=i+1)
        {
            q=1/i;
            b=b+q;
            cout<<"次数为"<<i<<" 结果为"<<b<<endl;//该句测试时删去
        }

        cout<<i;
        return 0;
    }

by SadLava @ 2019-11-09 21:34:31

(为甚么不用double)


by LingYun8 @ 2019-11-09 22:28:59


#include<bits/stdc++.h>
#include<iostream>
using namespace std;
int main()
{
    float b=1,q,i=2;
    int K;
    cin>>K;
    for (i;b<=K; i=i+1)
    {
        q=1/i;
        b=b+q;

    }
    i=--i;
    cout<<i;
return 0;
}

**60分**

by Suuon_Kanderu @ 2019-11-23 20:57:23

i为啥从2开始?

i=1开始

#include <bits/stdc++.h>
 using namespace std;
int main() {
    int k,i=0;
    cin>>k; 
   for(double s=0;s<=k;i++,s=1.0/i+s);
    cout<<i<<endl;
    return 0;
}

by Suuon_Kanderu @ 2019-11-23 20:59:40

而且

float精度不够

这是主要原因


|