错误代码(用函数真的多此一举了orz):
#include<iostream>
using namespace std;
int n, k;
double s(int x) {
double sum=0;
for (int i=1;i<=x;i++)
{
sum += 1.0/i;
}
return sum;
}
int main() {
cin >> k;
n = k;
while (s(n) <= k)
{
n++;
}
cout << n;
return 0;
}
AC代码:
#include <iostream>
using namespace std;
int main(){
double k;
cin >> k;
double sum = 0;
for (double i = 1;; i ++){
sum += 1 / i;
if (sum > k){
cout << i << endl;
break;
}
}
return 0;
}
数论(调和级数):
#include<iostream>
#include<cmath>
const double gamma=0.5772156649;
int main() {
int k,n;
cin>>k;
n=exp(k-gamma)+0.5;
cout<<n;
return 0;
}