牛客算法基础noob58 无限长正整数排列字符串
题目描述
定义一个无限字符串 S = "123456789101112......",即由所有正整数依次拼接而成。珂朵莉想知道该字符串的第 n 个字符是什么。
输入描述
输入一个整数 n(1 ≤ n ≤ 1000)。输出描述
输出字符串 S 的第 n 个字符(数字)。
import java.util.Scanner;// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {public static void main(String[] args) {Scanner in = new Scanner(System.in);int n = in.nextInt();int digit = 1; // 当前数字位数(1位、2位、3位...)long start = 1; // 当前位数的起始数字long count = 9; // 当前位数的总数字个数(如1位数字有9个)// 找到 n 所在的“位数区间”while (n > digit * count) {n -= digit * count;digit++;count *= 10;start *= 10;}// 找到目标数字start += (n - 1) / digit;String s = Long.toString(start);System.out.println(s.charAt((n - 1) % digit));}}