【入门】求恰好使s=1+1/2+1/3+…+1/n的值大于X时n的值。c++代码
·
描述
求恰好使s=1+1/2+1/3+…+1/n的值大于X时n的值。(2<=x<=10)
输入描述
输入只有一行,包括1个整数X。
输出描述
输出只有一行(这意味着末尾有一个回车符号),包括1个整数。
用例输入 1
2
用例输出 1
4
来源
需要找规律的循环
代码
#include <bits/stdc++.h>
#include<string>
#include<algorithm>
using namespace std;
int x;
int sh(int n,double s){
s+=1.0/n;
if(s>x){
return n;
}
else{
return sh(n+1,s);
}
}
int main(){
cin>>x;
cout<<sh(1,0);
return 0;
}
更多推荐
所有评论(0)