[NOIP2002 Popularization Group] Sum of Series

Topic link

Title description
Known: S n = 1+1/2+1/3+…+1/n. Obviously for any integer k, when n is large enough, S n > k.

Now given an integer kk, it is required to calculate a minimum n such that S n > k.

Input format
A positive integer k.

Output format
A positive integer n.

Input and output sample
input

1

Output

2

Code:

#include<iostream>
using namespace std;
int main()
{
    
    
	int k;
	cin >> k;
	double S = 0, i = 1.0;
	for(; S <= k; i++)
		S += 1 / i;
	cout << i - 1 << endl;
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_44826711/article/details/113725572