AcWing 57 數字序列中某一位的數字

2021-09-21 07:30:04 字數 500 閱讀 9523

數字以0123456789101112131415…的格式序列化到乙個字串行中。

在這個序列中,第5位(從0開始計數)是5,第13位是1,第19位是4,等等。

請寫乙個函式求任意位對應的數字。

樣例

輸入:13

輸出:1

用變數 i 表示是幾位數,s 表示 i位數有多少個,base表示 i位數的起始數。while迴圈退出後,i,s,base表示答案所在位置的情況。number表示答案在這個數中,wei表示答案在number中的位置(從0開始)。最後計算出ans返回。

class solution 

int number = base + (n - 1) / i;

int wei = (n - 1) % i;

int ans = 0;

for(int j = 0; j < i - wei; j++)

return ans;

}};

數字序列中某一位的數字

面試題44 數字序列中某一位的數字 題目 數字以0123456789101112131415 的格式序列化到乙個字串行中。在這 個序列中,第5位 從0開始計數 是5,第13位是1,第19位是4,等等。請寫一 個函式求任意位對應的數字。static int digit at uint32 t inde...

數字序列中某一位的數字

012345678910.序列中,第n位數 def solution n if n 0 return 1 if n 10 return n i 2 確定是第幾位數 sums 10 ret 0while true 一位數 0 9 共10個字元 兩位數 10 99 共180個字元 三位數 100 999...

數字序列中某一位的數字

數字以0123456789101112131415 的格式序列化到乙個字串行中,在這個序列中。第五位 從0開始計數 是5,第13位是1,第19位是4,等等。請寫乙個函式,求任意第n位對應的數字。暴力解法,從0開始計數,數到第n位。以1001位為例子。因為序列前10位是0 9這10個只有1位的數字。序...