why小白求解

P1035 [NOIP2002 普及组] 级数求和

i7889 @ 2020-10-26 10:50:51

include<stdio.h>

include <stdlib.h>


 int main() {
    double s=0;
    int k,i=0;
    scanf("%d",&k);
    while(1){
        i++;
        s=s+1.0/i;
        if(s>k)
        break;
    }
    printf("%d",&i);
    return 0; 
} ```

by 天南星魔芋 @ 2020-10-26 10:54:14

@i7889 i是int 型 s+1.0/i 自动向下去整了吧


by i7889 @ 2020-10-26 10:55:13

我不是用了1.0了吗


by 天南星魔芋 @ 2020-10-26 10:56:10

是 取不是 去


by 天南星魔芋 @ 2020-10-26 10:56:30

i是整型


by 天南星魔芋 @ 2020-10-26 10:57:07

//这样就好了
#include<bits/stdc++.h>
using namespace std;
 int main() {
    double s=0;
    int k;
    double i=0;
    scanf("%d",&k);
    while(1){
        i++;
        s=s+1.0/i;
        if(s>k)
        break;
    }
    cout<<i<<endl;
    return 0; 
}

by i7889 @ 2020-10-26 10:58:14

输入什么结果都是2487568


by 天南星魔芋 @ 2020-10-26 11:00:52

???


by i7889 @ 2020-10-26 11:03:33

@天南星魔芋 1.0/i中不应该有一个浮数型就可以了吗


by 天南星魔芋 @ 2020-10-26 11:04:27

貌似不是吧


by 天南星魔芋 @ 2020-10-26 11:05:20

@i7889

#include<bits/stdc++.h>
using namespace std;
 int main() {
    double s=0;
    int k;
    int i=0;
    scanf("%d",&k);
    while(1){
        i++;
        s=s+(double)(1.0/i);
        if(s>k)
        break;
    }
    cout<<i<<endl;
    return 0; 
}

这样也行


| 下一页