已AC,求优化

P1035 [NOIP2002 普及组] 级数求和

青鸣鸿 @ 2023-07-09 10:30:20


#include <iostream>
using namespace std;

int main() {
    int k;
    double n, a, m, i, b;
    cin >> k;
    n = 0;
    m = 1.0;
    i = 1.0;
    a = 0;
    do {
        a = m / i;
        n = n + a;
        i += 1;
    } while (n <= k);
    b = i - 1.0;
    cout << b << endl;
    return 0;
}

by SCYscy2580 @ 2023-07-09 10:32:55

打表(憋笑)(憋笑)(憋笑)


by zzhzsx @ 2023-07-09 10:38:56

@青鸣鸿 ???


by MTF_Lambda_04 @ 2023-07-09 10:51:59

@青鸣鸿

this?

#include<bits/stdc++.h>
using namespace std;
int main(){
    int k;
    double s=0,l=0;
    cin>>k;
    while(s<=k){
        l++;
        s=s+1/l;
    }
    cout<<l;

    return 0;
}

by steelseries @ 2023-08-02 11:49:09

打表.jpg

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a;
    cin>>a;
    if(a==1)cout<<"2";
    if(a==2)cout<<"4";
    if(a==3)cout<<"11";
    if(a==4)cout<<"31";
    if(a==5)cout<<"83";
    if(a==6)cout<<"227";
    if(a==7)cout<<"616";
    if(a==8)cout<<"1674";
    if(a==9)cout<<"4550";
    if(a==10)cout<<"12367";
    if(a==11)cout<<"33617";
    if(a==12)cout<<"91380";
    if(a==13)cout<<"248397";
    if(a==14)cout<<"675214";
    if(a==15)cout<<"1835421";
    return 0;
}

by Andy_hpy @ 2023-09-13 15:19:28

@kristopher 不需要那么麻烦

#include<bits/stdc++.h>
using namespace std;
int s[]={2,4,11,31,83,227,616,1674,4550,12367,33617,91380,248397,675214,1835421};
int main(){
    int k;
    cin>>k;
    cout<<s[k+1];
    return 0;
}

|