HDU1597 find the nth digit【模擬】
阿新 • • 發佈:2018-12-03
find the nth digit
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15975 Accepted Submission(s): 5044
Problem Description
假設:
S1 = 1
S2 = 12
S3 = 123
S4 = 1234
.........
S9 = 123456789
S10 = 1234567891
S11 = 12345678912
............
S18 = 123456789123456789
..................
現在我們把所有的串連線起來
S = 1121231234.......123456789123456789112345678912.........
那麼你能告訴我在S串中的第N個數字是多少嗎?
Input
輸入首先是一個數字K,代表有K次詢問。
接下來的K行每行有一個整數N(1 <= N < 2^31)。
Output
對於每個N,輸出S中第N個對應的數字.
Sample Input
6
1
2
3
4
5
10
Sample Output
1
1
2
1
2
4
Author
8600
Source
HDU 2007-Spring Programming Contest - Warm Up (1)
問題連結:HDU1597 find the nth digit
問題描述:S1=1,S2=12,S3=123,... ,當長度大於9時就進行1-9的迴圈。將所有的字串拼接起來,S1S2...Sm。給定n,問n位置的數是什麼
解題思路:模擬。先確定n在哪個字串,然後確定對應的值,具體看程式。
AC的C++程式:
#include<iostream> #include<cstdio> #include<algorithm> using namespace std; int main() { int n,t; scanf("%d",&t); while(t--) { scanf("%d",&n); int l=1;//l表示字串的長度,第一個字串的長度為1 while(n>l) { n-=l; l++;//獲取下一個字串的長度 } n%=9;//獲取n在此字串的位置 if(n==0) n=9; //如果n=0表示n的位置為9 printf("%d\n",n); } return 0; }