- 级数求和
- 2016-07-08 14:45:40 @
评测结果
编译成功
测试数据 #0: Accepted, time = 0 ms, mem = 504 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 500 KiB, score = 10
测试数据 #2: TimeLimitExceeded, time = 1203 ms, mem = 496 KiB, score = 0
测试数据 #3: Accepted, time = 500 ms, mem = 504 KiB, score = 10
测试数据 #4: TimeLimitExceeded, time = 1203 ms, mem = 496 KiB, score = 0
TimeLimitExceeded, time = 2906 ms, mem = 504 KiB, score = 30
代码
#include <cstdio>
int main() {
int k;
scanf("%d",&k);
for (int n = 1;true;n++) {
double sum = 1;
for (int i = 2;i <= n;i++)
sum += 1.0/double(i);
if (sum > k) {
printf("%d",n);
return 0;
}
}
return 0;
}
4 条评论
-
渔舟唱晚 LV 7 @ 2017-08-19 21:33:00
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <cstdlib>
using namespace std;
int k,n;
double sn=0.00;
int main() {
cin>>k;
sn=0.00;
for(n=1; sn<=k; n++) {
sn+=(double)(1/(double)n);
//cout<<sn<<endl;
}
cout<<n-1<<endl;
return 0;
}//O(n)算法,否则超时
-
2016-10-09 18:56:51@
思路太简单,不是很懂大牛做法,求思路谢谢。
~~~c++
#include<iostream>
using namespace std;
double k,n,s;
int main(){
cin>>k;
while(s<=k){
s+=1.0/++n;
}
cout<<n;
}
~~~ -
2016-07-08 15:29:13@
不错
-
2016-07-08 14:49:05@
AC了 评测结果 编译成功 测试数据 #0: Accepted, time = 0 ms, mem = 500 KiB, score = 10 测试数据 #1: Accepted, time = 0 ms, mem = 500 KiB, score = 10 测试数据 #2: Accepted, time = 15 ms, mem = 504 KiB, score = 10 测试数据 #3: Accepted, time = 0 ms, mem = 500 KiB, score = 10 测试数据 #4: Accepted, time = 0 ms, mem = 500 KiB, score = 10 Accepted, time = 15 ms, mem = 504 KiB, score = 50 代码 #include <cstdio> int main() { int k; scanf("%d",&k); double sum = 1; for (int n = 1;true;n++) { if (n == 1) continue; sum += 1.0/double(n); if (sum > k) { printf("%d",n); return 0; } } return 0; }
- 1