多了一个吧
by stray_233 @ 2017-11-16 21:02:35
```cpp
#include<iostream>
using namespace std;
double tot;
int i,k;
int main(){
cin>>k;
tot=0;
for(i=1;tot<=k;i++)
tot+=1.0/i;
cout<<i-1<<endl;//有一个是1.0/1所以要减一
return 0;
}
```
by stray_233 @ 2017-11-16 21:05:20
@[qq1141120715](/space/show?uid=67525) @[stray\_233](/space/show?uid=72798)
首先 , 你输出时多了一个 1 ,这是因为你的循环设置的不对! 你是看看先在是不是小,然后再加上 1/i ,但是正确的是改反过来,还有就是 你的double 类型转换的问题, 不然还是int 类型!
```cpp
#include <stdio.h>
#include <stdlib.h>
int main()
{
int k,n=0;
double sum = 0.0000;
scanf("%d",&k);
for(n = 1;sum<=(double)k;n++)
{
sum = sum +(1.0/(double)n);
}
printf("%d\n",n-1);
return 0;
}
```
by Bartholomew @ 2017-11-17 10:51:21
@[Bartholomew](/space/show?uid=50047)
不是自动就可以转化的吗?
by stray_233 @ 2017-11-17 19:45:21
@[Bartholomew](/space/show?uid=50047)
你这是c语言?
by stray_233 @ 2017-11-17 19:45:50
这是C++(汗...)
有时候需要强制进制转换的! 可能现在不需要,但是之后又必要的!
by Bartholomew @ 2017-11-17 20:53:19
@[stray\_233](/space/show?uid=72798) 只是这样比较的保险!
by Bartholomew @ 2017-11-17 20:55:31