- 级数求和
n++和++n咋弄啊
- 2024-12-17 13:58:51 @
**正确**
#include <bits/stdc++.h>
using namespace std;
int main()
{
int k,n=0;
double s=0;
cin>>k;
while(s<=k*1.0)
{
s+=1.0/++n;
}
cout<<n;
return 0;
}
**错误**
```cpp
#include <bits/stdc++.h>
using namespace std;
int main()
{
int k,n=0;
double s=0;
cin>>k;
while(s<=k*1.0)
{
s+=1.0/n++;
}
cout<<n;
return 0;
}
要炸了
2 comments
-
LHY LV 10 @ 2024-12-21 13:43:33
n++的用法是先判断再+1,++n是先+1在判断,比如n=-3,语句while(n++)会运行3次,while(++n)会运行2次
-
2024-12-17 16:19:06@
++n是前置加,就是先n+1再进行运算,n++是后置加,就是先运算再n+1
- 1
Information
- ID
- 581
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 5
- Tags
- (None)
- # Submissions
- 308
- Accepted
- 114
- Uploaded By