5个RE,求问大佬怎么改?

P1035 [NOIP2002 普及组] 级数求和

long_long @ 2019-11-03 16:48:53

#include <iostream>
#include <cstdio>
using namespace std;
int n, ans=1;
int main(){
    cin>>n;
    for(int i=0; i<n; i++){
        ans+=1/i;
    }
    cout>>ans;
    return 0;
}

by zjjws @ 2019-11-03 16:50:51

i不要从0开始

一个数/0是会RE的


by long_long @ 2019-11-03 16:51:59

@zjjws 谢谢大佬,AC了


by long_long @ 2019-11-03 16:53:33

@zjjws 大佬,i<n怎么改?


by zjjws @ 2019-11-03 16:56:33

@long_long ?


by zjjws @ 2019-11-03 16:58:12

@long_long

我看了下题目发现你这个代码好玄学


by zjjws @ 2019-11-03 16:58:51

@long_long

这题不是这个意思的吧


by zjjws @ 2019-11-03 17:00:11

不是给你n叫你求Sn

而是给你K求最小的n使Sn>k


by zjjws @ 2019-11-03 17:04:05

刚刚写的满分代码

#include <bits/stdc++.h>
#define LL long long
using namespace std;
LL rin()
{
    LL s=0;
    bool bj=0;
    char c=getchar();
    while((c>'9'||c<'0')&&c!='-')c=getchar();
    if(c=='-')bj=1,c=getchar();
    while(c>='0'&&c<='9')s=s*10+c-'0',c=getchar();
    if(bj)return -s;
    return s;
}
int main()
{
    int i,j;
    int k=rin();
    double s=1;
    for(i=2;s<=k;i++)
    {
        double z=1;
        z/=i;
        s+=z;
    }
    i--;
    printf("%d",i);

    return 0;
}

by 编程萌新or大神 @ 2019-11-09 13:42:27

不能从0


|