快崩溃了,到底哪里要改QAQ

P3612 [USACO17JAN] Secret Cow Code S

bluetored @ 2023-03-01 17:15:27

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
long long n,llen,len;
string a;
char fun(long long x,long long l){
    if(x<len) return a[x];
    if(x>l){
        if(x==l+1) return fun(l,l/2);
        return fun(x-l-1,l/2);
    }
    return fun(x,l/2);
}
int main ()
{
    cin>>a>>n;
    a=" "+a;
    len=a.size()-1;
    llen=len;
    while(llen<n*1.0/2) llen*=2;
    cout<<fun(n,llen);
    return 0;
}

by LEZ2012 @ 2023-04-03 20:46:31

@bluetored


#include <bits/stdc++.h>
using namespace std;
string s;
long long n,num,i;
int main()
{
    cin>>s>>n;
    num=s.length(); 
    while(num<n)
    {
        i=num;
        while(n>i)  i*=2;
        i=i/2;
        n-=(i+1); 
        if(n==0)    n=i;
    }
    cout<<s[n-1];
}

by LEZ2012 @ 2023-04-03 20:47:01


求关注

by jackqhr01 @ 2023-07-12 10:10:24

@bluetored 只除一个2是不够的,我也在这里被坑了好几次。如

$output$ _A_ ```cpp char find(LL k,LL l){ if(k>=1&&k<=len){ return x[k]; } LL a=k-l-1,b=l; if(a==0) a=b; while(b>=a) b=b>>1; find(a,b); } ```

|