死循环,求调

P3612 [USACO17JAN] Secret Cow Code S

Jasper20222022 @ 2024-03-17 15:45:33

#include <bits/stdc++.h>
using namespace std;
string func(string s){
    string s2;
    for(int i=1;i<s.size();i++){
        s2[i]=s[i-1];
    }
    s2[0]=s[s.size()-1];
    return s2;
}
int main(){
    string s;
    cin>>s;
    long long n;
    cin>>n;
    while(s.size()<n){
        s+=func(s);
    }
    cout<<s[n];
    return 0;
} 

by J_ALVAREZ @ 2024-04-06 17:28:59

@Jasper20222022 暴力会超时


by J_ALVAREZ @ 2024-04-06 17:29:18

@Jasper20222022 看题解


by Kete @ 2024-07-23 09:03:18

MeのAC-CODE:

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

by Kete @ 2024-07-23 09:03:51

就那么短


|