1. 程式人生 > >hd 1597 find the nth digit(解決超時的牛氣哄哄的新辦法)

hd 1597 find the nth digit(解決超時的牛氣哄哄的新辦法)

<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 下,所有的變數都改著試試