hdoj 1597 find the nth digit 大水題!!

                           find the nth digit

                             Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
                                                   Total Submission(s): 8404    Accepted Submission(s): 2398


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
 
AC CODE:
#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    long long int sum,temp,j,i,k;
    __int64 n;
    scanf("%d",&k);
    while(k--)
    {
        scanf("%I64d",&n);
        for(i=(int)sqrt(2*n)-1;;i++)
        if(i*(i+1)>=2*n)
        break;
        //printf("%d#\n",i);
        temp=(i-1)*i/2;
        //if(n>temp)
        n-=temp;
        if(n%9==0)
        printf("9\n");
        else
        printf("%d\n",n%9);
    }
    return 0;
}

題中說不超過64位,是個坑!!不用64位整形提交過不了,哎~大水題,不解釋。。
發佈了67 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章