<h1 style="COLOR: #1a5cc8"><div class="panel_title" align="left"><h1 style="COLOR: #1a5cc8">find the nth digit</h1></div><span size="+0"><strong><span style="font-family:Arial;font-size:12px;color:green;FONT-WEIGHT: bold">Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8385 Accepted Submission(s): 2394
</span></strong></span>
</h1><div class="panel_title" align="left">Problem Description</div><div class="panel_content">假设:
S1 = 1
S2 = 12
S3 = 123
S4 = 1234
.........
S9 = 123456789
S10 = 1234567891
S11 = 12345678912
............
S18 = 123456789123456789
..................
现在我们把所有的串连接起来
S = 1121231234.......123456789123456789112345678912.........
那么你能告诉我在S串中的第N个数字是多少吗?
</div><div class="panel_bottom"> </div>
<div class="panel_title" align="left">Input</div><div class="panel_content">输入首先是一个数字K,代表有K次询问。
接下来的K行每行有一个整数N(1 <= N < 2^31)。</div><div class="panel_bottom"> </div>
<div class="panel_title" align="left">Output</div><div class="panel_content">对于每个N,输出S中第N个对应的数字.
</div><div class="panel_bottom"> </div>
<div class="panel_title" align="left">Sample Input</div><div class="panel_content"><pre><div style="FONT-FAMILY: Courier New,Courier,monospace">6
1
2
3
4
5
10</div>
Sample Output
1 1 2 1 2 4
Author
8600
Source
Recommend
#include<stdio.h>
//#include<iostream>
#include<math.h>
//using namespace std;
int main()
{
__int64 n,t,i,s,p;
scanf("%d",&t);
while(t--)
{
scanf("%I64d",&n);
for(i=(int)sqrt(n*2)-1;;i++)
{
if(i*(i+1)/2>=n)
{
p=i;
break;
}
}
s=(p-1)*p/2;
if(n>s)
n-=s;
while(n>9)
{
n-=9;
}
printf("%d\n",n);
}
return 0;
}
//加上限制条件i<n试试,不要想当然,多思
//注意初始限制条件i=sqrt(n*2)-1;
//超时时如果确认过程正确的话试着把iint 改__int64 下,所有的变量都改着试试