HDU 1597 find the nth digit
阿新 • • 發佈:2019-02-11
題目:
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
Sample Input
6 1 2 3 4 5 10Sample Output
1 1 2 1 2 4這個題目沒什麼難度,規律很明顯,直接數學求解就是的了。
12分鐘就AC了,終於有個題目是我最先做出來的了,不容易啊啊啊啊,他們太強了。
程式碼:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int k;
cin >> k;
int n;
while (k--)
{
cin >> n;
double x = sqrt(n*2.0);
long long a = int(x) - 1;
while (a*(a + 1)/2 < n)a++;
a--;
long long b = n - a*(a + 1) / 2;
cout << (b - 1) % 9 + 1 << endl;
}
return 0;
}