50

P3612 [USACO17JAN] Secret Cow Code S

Lhy2009 @ 2020-12-28 19:48:16

#include<iostream>
#include<string>
using namespace std;
long long did(long long n,long long l){
    if(n<=l){
        return (int)n;
    }
    int k=did(n,2*l);
    if(k<=l){
        return k;
    }else if(n==l+1){
        return l;
    }else{
        return k-l-1;
    }
}
int main(){
    string x;
    long long n;
    cin>>x;
    cin>>n;
    n=did(n,x.length());
    cout<<x[n-1]<<endl;
    return 0;
}

by LoneWolf @ 2020-12-28 20:38:29

@Lhy2009 咋了,啥意思?


by Lhy2009 @ 2020-12-29 14:30:57

50分,哪里错了


by Lhy2009 @ 2020-12-29 15:05:06

更新代码,60分(6ac+2re+2wa)

#include<iostream>
#include<string>
using namespace std;
long long did(long long n,long long l){
    if(n<=l){
        return n;
    }
    int k=did(n,l<<1);
    if(k<=l){
        return k;
    }else if(k==l+1){
        return l;
    }else{
        return k-l-1;
    }
}
int main(){
    string x;
    long long n;
    cin>>x;
    cin>>n;
    int k=did(n,x.length());
    cout<<x[k-1]<<endl;
    return 0;
}

by Lhy2009 @ 2020-12-29 15:15:51

sorry,本帖终(100分)

#include<iostream>
#include<string>
using namespace std;
int main(){
    string x;
    long long n;
    cin>>x;
    cin>>n;
    long long lx=x.length();
    int m=0;
    while(n>lx){
        lx*=2;
        m++;
    }
    for(int i=0;i<m;i++){
        lx/=2;
        if(n<=lx){

        }else if(n==lx+1){
            n=lx;
        }else{
            n-=(lx+1);
        }
    }
    cout<<x[n-1]<<endl;
    return 0;
}

|