首页 技术 正文
技术 2022年11月15日
0 收藏 375 点赞 2,462 浏览 1448 个字

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1597

find the nth digit

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

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 Input6
1
2
3
4
5
10 Sample Output1
1
2
1
2
4 Author8600题解:可以用二分解,也可以用二次方程公式解,要注意使用long long二分查找的代码:

 #include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define ll long long
ll f(ll x)
{
ll ans = x*(x+)/ ;
return ans;
}
int main()
{
int T;
scanf("%d",&T);
for(int i = ;i < T ;i++)
{
ll n ;
scanf("%lld",&n);
if(n==)
{
puts("");
continue;
}
ll l = ;
ll r = n;
ll mid =( l+r )/;
int tm = ;
while(r-l>)
{
if( f(mid) < n )
{
l = mid;
mid = (l+r)/;
}
else if( f(mid) > n)
{
r = mid;
mid = (l+r)/;
}
else if( f(mid)==n )
{
tm = mid;
break;
}
}
if(tm!=)
{
if(tm%==) puts("");
else
printf("%d\n",tm%);
}
else
{
ll flag = l;
ll tt = flag*(flag+)/;
int sum = n-tt;
if(sum%==) puts("");
else
printf("%d\n",sum%);
}
}
return ;
}

公式代码:

 #include<cstdio>
#include<cmath>
using namespace std;
#define ll long long
#define eps 1e-8 int main()
{
int T;
scanf("%d",&T);
for(int i = ;i < T;i++)
{
double n;
scanf("%lf",&n);
double m =(sqrt(+*n)-)/;
ll mm =m;
ll tm = mm*(mm+)/;
ll flag = n-tm;
if(flag == ){if(mm%==)printf("9\n");else printf("%lld\n",mm%);}
else
{
if(flag%==) printf("9\n");
else
printf("%lld\n",flag%);
}
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,499
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,914
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,746
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,503
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,142
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,305