为啥还有俩过不了啊啊啊啊啊啊啊

P1035 [NOIP2002 普及组] 级数求和

I_love_zz @ 2024-09-11 21:52:17

听取了大佬们的教导
知道了一件重要的事
1 / i = 0
所以优化了一下,对了几个
但是还是有点问题
(而且不知道为啥scanf用不了,注释掉了)
只能再次求助大佬

垃圾代码~~~

#include<iostream>
#include<cstdio>
using namespace std;
int main() {
    int k = 0, n = 0, b = 0;
    float a = 0;
    cin >> k;
    //scanf("%d", &k);
    for (int i = 1; a <= k; i++) {
        a += (1.0 / i);
        b++;
    }
    printf("%d", b);
    return 0;
}

by SmileString @ 2024-09-11 21:55:54

@I_love_zz 哥们,好好看看题目吧


by SmileString @ 2024-09-11 21:56:21

@I_love_zz 用while


by I_love_zz @ 2024-09-11 21:59:30

@Dream66666 用for也行吧···


by coderzhb @ 2024-09-11 22:07:08

可以试试把float换成double


by FQY_0307 @ 2024-09-11 22:26:52

@Dream66666

6,众所周知(看起来有人不知)所有的while和for都可以相互转换


by SmileString @ 2024-09-15 13:51:53

@FQY_0307 说得对,但是本题用while不是更方便吗?


by FQY_0307 @ 2024-09-15 20:54:53

@Dream66666 没错 :) 赞!


by yangzijin @ 2024-09-18 22:32:16

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

|