求助大佬,我哪里错了

P1035 [NOIP2002 普及组] 级数求和

JoyBoy521 @ 2023-02-27 21:06:46

#include<iostream>
using namespace std;
int main()
{
 int k;
 int n=1;
 double sum=0;
 cin>>k;
 for(n=1;sum<=k;n++)
{
 sum=sum+1.0/n;

}
 cout<<n;
 return 0;
}

by jiangjiangQwQ @ 2023-02-27 21:42:35

还是回去仔细看看题目吧,你的题都没看明白呀


by JoyBoy521 @ 2023-02-28 19:28:11

@jiangshuhong 我感觉我没看错题,咋回事。。。。。这道题把我整迷糊了


by jiangjiangQwQ @ 2023-02-28 19:43:09

#include<iostream>

using namespace std;
int main() {
    int k,n;
    double sum = 0;
    cin >> k;
    for (n = 0; sum <= k;) {
        ++n;
        sum = sum + (double)1.00 / n;
    }
    cout << n;
    return 0;
}

直接这样改了就能过


by jiangjiangQwQ @ 2023-02-28 19:44:14

@JoyBoy521 雀使,我WA了几遍才调出来(恼


by jiangjiangQwQ @ 2023-02-28 19:49:23

主要就是要sum哪里要double类型转换...


by JoyBoy521 @ 2023-02-28 19:50:17

@jiangshuhong 谢谢大佬


|