GOODSSouth @ 2023-05-02 16:15:10
#include<stdio.h>
int main()
{
int a=1,N;
double b=0.0;
scanf("%d",&N);
for(;b<N;)
{
b=1.0/a+b;
a++;
}
printf("%d",a);
return 0;
}
by _buzhidao_ @ 2023-05-02 16:28:12
#include<bits/stdc++.h>
using namespace std;
int k,n;double deshu;
int main(){
cin>>k;
while(deshu<=k){
n+=1;
deshu+=1.0/n;
}
cout<<n;
return 0;
}
by pengyuanxi @ 2023-05-02 16:42:45
@GOODSSouth
#include<bits/stdc++.h>
using namespace std;
int k,n;
double s;
int main()
{
cin>>k;
while(k>=s)
{
n++;
s=s+1.0/n;
}
cout<<n;
return 0;
}
求关注
by LTY_2020 @ 2023-05-17 10:39:12
不妨把a的初始化改为0,这样可以从1.0/1 开始计数,这样的话就需要把a++移动到b += 1.0/a之前了
by WhisperingWillow @ 2023-05-25 11:35:54
@buzhidao 这道题应用线段树解决。
第一步:建树
第二步:循环找答案
代码如下:(1/a[i]有问题,自己改,要不然被举报在评论区发AC code就不好了)
#include<iostream>
#define int double
#define intt long
using namespace std;
//这道题是线段树的好题
using namespace std;
struct node{
int val,l,r;
};
const long N=(1e6+1)*4;
node t[N];
int a[N],f[N];
int n,m;
void build(intt l,intt r,intt node){
t[node].l=l;t[node].r=r;t[node].val=0;
if(l==r){
f[l]=node;
t[node].val=a[l];
return;
}
register int mid=(l+r)>>1;
build(l,mid,node*2);
build(mid+1,r,node*2+1);
t[node].val=t[node*2].val+t[node*2+1].val;
}
void update(intt node){
if(node==1)return;
intt fa=node>>1;
t[fa].val=t[fa*2].val+t[fa*2+1].val;
update(fa);
}
int find(intt l,intt r,intt node){
if(t[node].l==l&&t[node].r==r){
return t[node].val;
}
register int sum=0;
register intt lc=node*2;
register intt rc=lc+1;
if(t[lc].r>=l){
if(t[lc].r>=r){
sum+=find(l,r,lc);
}
else{
sum+=find(l,t[lc].r,lc);
}
}
if(t[rc].l<=r){
if(t[rc].l<=l){
sum+=find(l,r,rc);
}
else{
sum+=find(t[rc].l,r,rc);
}
}
return sum;
}
signed main(){
register int k;
cin>>k;
for(register intt i=1;i<=1e6;i++) a[i]=1/(int)i;
build(1,1e6,1);
//很明显,线段树求和
for(register intt i=1;i<1e6;i++){
if((int)find(1,i,1)>k){
cout<<i;
return 0;
}
}
}
by _buzhidao_ @ 2023-05-25 21:02:55
@jordendujunrui 这是一道入!门!题!
by WhisperingWillow @ 2023-05-25 21:56:07
@buzhidao 这不是提!高!题!吗?
by _buzhidao_ @ 2023-05-26 20:38:12
@jordendujunrui 我看到是入门,不是P1035吗?
by WhisperingWillow @ 2023-05-26 20:47:40
@buzhidao 看不出来我在开玩笑吗?
by _buzhidao_ @ 2023-05-26 20:48:55
@jordendujunrui .
by WhisperingWillow @ 2023-05-26 20:51:18
@buzhidao ??