268 条题解
-
5
啡咖啡咖啡咖啡咖 LV 8 @ 2018-08-18 09:54:34
#include<cstdio> int a,b,c; double ans; int main() { scanf("%d",&a); int i=1; while(ans<=a) { ans+=1.0/i; i++; } printf("%d",i-1); return 0; }
-
22018-08-18 09:52:33@
#include<iostream> using namespace std; int main() { double an=0; int a,i=1; cin>>a; while(an<=a) { an+=1.0/i; i++; } cout<<i-1; return 0; }
-
12022-04-07 20:45:46@
#include<iostream> using namespace std; int a,b,c; double d; int main() { cin>>a; int i=1; while(d<=a) { d+=1.0/i; i++; } cout<<i-1; return 0; }
-
12021-07-23 08:49:58@
#include<bits/stdc++.h>//万能头文件
using namespace std;
double x;
int s,i;
//把变量定义成全局变量,变量一开始值都是0
int main(){
cin>>s;//输入
for(i=1;;i++){
x=x+1.0*1/i;//累加
if(x>s)break;//达到条件后就可以退出循环了
}
cout<<i;//输出
return 0;//在NOIP考场上,不写return 0会爆0的!!!所以return 0很重要!
} -
12018-09-05 21:22:21@
lo
```cpp
#include<iostream>
using namespace std;int main(){
int k;
double tot,i;
cin>>k;
i=0.00000;
tot=0.00000;
while(tot<=k){
i++;
tot+=1/i;
}
cout<<i<<endl;
}
``` -
02022-04-10 08:16:56@
#include<iostream>
using namespace std;
int main(){
int k;
cin>>k;
double Sn=0;
int i=0;
while(Sn<=k){
i++;
Sn+=1.0/i;
}
cout<<i<<endl;
return 0;
} -
02022-04-10 08:16:05@
#include<iostream>
using namespace std;
int main(){
int k;
cin>>k;
double Sn=0;
int i=0;
while(Sn<=k){
i++;
Sn+=1.0/i;
}
cout<<i<<endl;
return 0;
} -
02022-01-16 10:30:40@
思路:
1.这道题无法判断要执行多少次,因此要用
while
循环. ~~for(;;)
表示不服~~2.题目问的是大于k,因此**终止**条件应该是
sum>k
,也就是说循环条件应该是sum<=k
.3.还要注意一些
while
循环的+1
-1
代码:
#include<iostream> #include<cstdio> using namespace std; int k,n; double sum;//sum用于表示当前的和 int main(){ scanf("%d",&k); while(sum<=k)//是小于等于,不是小于 sum+=1.0/++n;//1.0将答案转为double类型 /* 也可以写成sum+=1.0/n++ 但这样写的话n要赋初值1 其实就等于 n++; sum+=1.0/n; */ printf("%d",n); return 0; }
-
02020-03-31 17:15:53@
#include<iostream>
using namespace std;
int main(){
double S=0;
int k,n=1;
cin>>k;
while(S<=k){
S+=1.0/n;
n++;
}
cout<<n-1;
} -
02020-03-19 00:23:16@
这个题看似是一个简单,但是会经常出现一种
思维漏洞
首先
题目问的是要大于**k**的数,也就是说我们不清楚要循环多少次,因为我们使用while循环,然后在
while循环中要使用题目的逆命题也就是要小于等于**k**, 但是由于精度问题当k与n相等的时候。n其实已经比k大了所以在,打印答案的时候就需要减去1才对// 级数求和 // https://vijos.org/p/1127 #include <bits/stdc++.h> using namespace std; int main() { int n = 1; int a = 0; double sum = 0; cin >> a; while (sum <= a) { sum += 1.0 / n; n++; } cout << n - 1 << endl; return 0; }
-
02020-03-17 19:03:20@
#include<iostream> using namespace std; int main () { int k, n = 1; double sn = 0; cin >> k; while(sn <= k) { sn += 1.0/n; n++; } cout << n - 1 << endl; return 0; }
-
02019-06-17 13:50:22@
#include <iostream>
using namespace std;
int main(void)
{
double k, n = 1.0, all = 0.0;
cin >> k;
while (all <= k) {
all += (1 / n);
++n;
}
cout << (--n);
return 0;
} -
02019-01-02 19:03:39@
#include<iostream> #include<cstdio> using namespace std; int main() { int n=1;double s=1,k; scanf("%lf",&k); do { n++; s+=1.00/n; } while(s<=k); cout<<n<<endl; return 0; }
-
02018-12-29 21:00:50@
还行,只要用到iostream就行。
c++#include <iostream> using namespace std ; int n ; double a[10000] , k ; int main() { cin >> n ; for ( int i = 1 ; /*sb*/ ; i ++ ) { k += 1.0 / i ; if ( k > n ) { cout << i << endl ; return 0 ; } } return 0 ; }
-
02018-11-03 20:55:18@
Pascal代码
var
s:real;
k,i:longint;
begin
read(k);
while s<=k do
begin
inc(i);
s:=s+1/i;
end;
write(i);
end. -
02018-10-01 22:05:34@
p党来了。
var
k,s:real;
i:longint;
begin
read(k);
while s<k do
begin
i:=i+1;
s:=s+1/i;
end;
write(i);
end. -
02018-09-13 20:19:22@
#include<bits/stdc++.h>
using namespace std;
int main()
{
double i=1,n=0,k;
cin>>k;
for(i=1;i>0;)
{
n+=1/i;
if(n<=k)
i++;
else break;
}
cout<<i;
return 0;
} -
02018-08-08 19:13:46@
Pascal党,请注意查收
var i,K:longint;
Sn:real;
begin
readln(K);
Sn:=0;
i:=0;
while Sn<=K do
begin
inc(i);
Sn:=Sn+1/i;
end;
writeln(i);
end. -
02018-08-07 16:06:00@
C++AC标程(2):
#include<bits/stdc++.h> //万能头文件,这是可以用的!!好开森!
using namespace std;
int main()
{
int k,i=0;
double s=0.0000;
cin>>k;
do
{
i++;
s=s+ (1.0/i);
}
while (s<=k);
printf("%d",i);
return 0;
} -
02016-09-17 10:17:57@
样例我虽不对,但满分