hd 1597 find the nth digit(解决超时的牛气哄哄的新办法)

本文介绍了一种算法,用于解决查找由连续整数构成的无限序列中任意位置的数字问题。通过数学推导和编程实现,展示了如何快速定位到序列中指定位置的数字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<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 下,所有的变量都改着试试 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值