njuoj 遞歸查詢字符串

 

 思路:每次將pos映射到對稱區間的相應位置。直到達到[1,5]區間java

其中須要注意的是,須要找到$的分佈規律。單獨處理spa

package com.company.dp;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class 遞歸查詢子字符串 {
    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int case_num=Integer.parseInt(scanner.nextLine());
        for (int i = 0; i < case_num; i++) {
            long pos=Long.parseLong(scanner.nextLine());
            char res=getNum(pos);
            System.out.println(res);
        }
        scanner.close();
    }

    private static char getNum(long pos) {
        long[] s=new long[50];
        s[0]=5;
        for (int i = 1; i < s.length; i++) {
            s[i]=s[i-1]*2+i;
            
        }
        for (int i = 0; i < s.length; i++) {
            System.out.println(s[i]);
        }

        String str0="12345";

        while(pos>5){
            for (int i = 1; i < s.length; i++) {
                if(pos>=s[i-1]&&pos<=s[i]){

                    if(2*pos-s[i]>=0&&2*pos-s[i]<=i)return '$';
                    pos=s[i]-pos+1;
                    break;
                }

            }

        }
        return str0.charAt(((int) pos)-1);
        
    }

}
相關文章
相關標籤/搜索