求#3的数据,一直是re

P3612 [USACO17JAN] Secret Cow Code S

dxudxu @ 2024-02-13 18:21:03

如题


by Naughty_little_black @ 2024-02-13 18:27:00

@dxudxu 发一下你RE的代码


by zhangbo1000 @ 2024-02-13 18:49:03

如果题目允许下载数据,请点击下载数据。

如果题目不允许,那么你大概率不应该得到数据,实在需要请私信出题人或管理。 @dxudxu


by dxudxu @ 2024-02-13 19:16:08

@wzr0416

import java.util.*;

public class Main
{
    public static void main(String[] args)
    {
        Scanner sc=new Scanner(System.in);
        String str=sc.next();
        long n=sc.nextLong();
        System.out.println(str.charAt((int) findIndex(n,str.length())-1));
    }

    public static long findIndex(long n,int length)
    {
        long len=length;
        while(len<n-len)
        {
            len*=2;
        }
        if(n<=length)
            return n;
        else
            return findIndex(n-len-1,length);
    }

}

by dxudxu @ 2024-02-13 19:16:40

@wzr0416 用java写的


by Naughty_little_black @ 2024-02-13 19:37:58

@dxudxu 输入是ABC 1000,输出是A(我学的是C++,帮不到您,Sorry)


by dxudxu @ 2024-02-14 13:44:45

@wzr0416 好的,谢谢!


by Naughty_little_black @ 2024-02-14 20:44:01

@dxudxu OK!


by Lanmou @ 2024-03-07 23:17:29

借楼问怎么会re


#include<bits/stdc++.h>
using namespace std;
long long n,m,l,k;
long long dfs(long long x){
    if(x<=m) return x;
    k=k/2;
    if(x%k==1) dfs(x-1);
    else dfs(x-k-1);
}
int main(){
    string s,ss;
    cin>>s>>n;
    k=m=s.length();
    while (k<n){
        k=k*2;
    }
    printf("%c",s[dfs(n)-1]);
    return 0;
//  abcdefg gabcdef fabcdefggabcde
}

by ALANYQ @ 2024-03-12 12:36:56

@Lanmou dfs函数只有x<=m时才会有返回值,在递归时要return dfs(x)。


by abc1234shi @ 2024-04-16 21:08:24

@Lanmou 加一条if(x<=k) return dfs(x);就行了


|