tm25 @ 2022-07-20 15:39:19
#include<bits/stdc++.h>
using namespace std;
int main(){
int n=0,k;
double a,s=0;
scanf("%d",&k);
while(s<=k)
{
n++;
a=1/n;
s+=a;
}
printf("%d",n);
return 0;
}
去掉中间值a,直接写1/n就过了,但不懂这个为啥不对,求助各位大佬
by LiaoYF @ 2022-07-20 15:58:09
@tm25 1/n得到的还是int类型啊,因为n是int,1也是int
by LiaoYF @ 2022-07-20 15:59:07
@tm25 改成a=1.0/n;
by tm25 @ 2022-07-21 18:14:34
@Mr_LiaoYifan 明白了,谢谢大佬
by Angus66 @ 2023-10-29 10:58:51
#include <bits/stdc++.h>
using namespace std;
int main(){
int a=0;
double num=0;
int i=0;
cin>>a;
while(num<a){
i++;
num=num+1.0/i;
}
cout<<i;
return 0;
}