題目
菜鳥倉庫是一個很大很神奇的地方,各種琳琅滿目的商品整整齊齊地擺放在一排排貨架上,通常一種品類(sku)的商品會放置在貨架的某一個格子中,格子設有統一的編號,方便工人們揀選。 有一天沐哲去菜鳥倉庫參觀,無意中發現第1個貨架格子編碼爲1,第2-3個分別爲1,2,第4-6個格子分別是1,2,3,第7-10個格子編號分別是1,2,3,4,每個格子編號都是0-9中的一個整數,且相鄰格子的編號連在一起有如下規律 1|12|123|1234|…|123456789101112131415|…|123456789101112131415……n 這個倉庫存放的商品品類非常豐富,共有1千萬多個貨架格子。沐哲很好奇,他想快速知道第k個格子編號是多少?
要求
輸入:
貨物的序號K,是一個整數
輸出:
貨物的編碼
輸入範例:
10
輸出範例:
4
分析
注意: 每一排放入自己所有的編號,例如‘10’表示兩個編號,所以要將整數10拆成1和0兩個字符串
代碼
#include <vector>
#include <climits>
#include <cstdio>
#include <string>
#include <iostream>
#include <algorithm>
#include <map>
#include <iomanip>
using namespace std;
int findNthDigit(int n) {
long long int a = 9;
long long int k = 1;
long long int dd = 1;
while (n>a*k) {
n -= a*k;
a = a * 10;
k++;
dd = dd * 10;
}
int m = (n - 1) / k;
int ll = k - (n - 1) % k - 1;
int now = dd + m;
while (ll--) {
now = now / 10;
}
return now % 10;
}
int f(int a) {
int ji = 0;
while (a) {
a = a / 10;
ji++;
}
return ji;
}
int main() {
int k;
cin >> k;
int a = 1;
int tmp = 1;
int res = 0;
while (a < k) {
res += a;
k = k - a;
tmp++;
a = a + f(tmp);
}
cout << findNthDigit(k);
//cout << tmp1 <<" "<< tmp2 << " "<<tmp3 << endl;
system("pause");
return 0;
}