20分

P1035 [NOIP2002 普及组] 级数求和

lrqSB @ 2024-08-23 09:32:57



#include <bits/stdc++.h>

using namespace std;
int main() {
int a,sum;

cin>>a;
for(int i=1;i<=a;i++){
    sum+=1/a;
}cout<<sum+1;
}

by lrqSB @ 2024-08-23 09:33:51

sum如果不加1,样例就不对了


by dream_dad @ 2024-08-23 09:39:45


#include <bits/stdc++.h>

using namespace std;
int main() {
int a,sum;

cin>>a;
for(int i=1;i<=a;i++){
    sum+=1/i;
}cout<<sum+1;
}

by dream_dad @ 2024-08-23 09:40:03

这样改


by JYC130604 @ 2024-08-23 10:27:07

不需要double吗?

#include  <bits/stdc++.h>
using namespace std;

int main(){
    int n=1;
    double sum=1.0,k;
    cin>>k;
    for(double i=2.0;sum<=k;i++){
        if(sum>k){
            break;
        }
        else{
            sum+=1/i;
            n++;
        }
    }
    cout<<n;
}

by liuyuhan1522 @ 2024-09-06 22:15:45

输出i,不是sum。
sum+=1.0 / i
sum开double


by liuyuhan1522 @ 2024-09-06 22:17:42

ac 代码

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

by liuyuhan1522 @ 2024-09-06 22:19:21

可以关注吗


by liuyuhan1522 @ 2024-09-06 22:20:08

@lion0617


|