无限序列-----112123123412345123456…………

在这里插入图片描述
在这里插入图片描述

Sample Input

5
1
3
20
38
56

Sample Output

1
2
5
2
0

解题思路以及关键代码

核心思路是主要是二分
首先分析序列:s1=1,s2=12,s3=123,s4=1234,……s10=12345678910
序列 s1s2s3s4……s9s10,可以通过二分的方法,首先确定属于哪个s中;
同时a[i]记录第i位数序列的最大值,b[i]记录最后一个i位数所对应的序列长度

在这里插入图片描述
①确定k属于几位数。
②通过二分查找确定 k属于 i位数序列的第几个。
③最后在这个序列中查找k的位置。

全部代码

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<queue> 
using namespace std;
long long a[20],b[20];

int func1()
{	
	a[0]=b[0]=0;
	long long count = 9;
	int i;
	for(i=1;a[i-1]<=(long long)pow(10,18);i++)
	{
		a[i]=((b[i-1]+i)+(b[i-1]+count*i))*count/2;  //s1s2....si共有多少位
		b[i]=b[i-1]+count*i;  //最后一个i位数所对应的序列长度 
		count*=10;
	}
	return i-1;
}
int main()
{
	long long q;
	scanf("%lld",&q);
	long long n=func1();
	while(q--)
	{
		long long k;
		scanf("%lld",&k);
		//确定 k 属于i位数 
		long long i=(long long)(lower_bound(a,a+n,k)-a);
		for(int j=1;j<i;j++)
			k=k - a[j];
		//确定 k 属于i位数序列的第几个   b[i]中记录了个数 
		long long l=1;
		long long r=(long long)(9*pow(10,i-1));
		long long mid;
		while(l<=r) //二分 
		{
			mid=(l+r)/2;
			long long tmp=((b[i-1]+i)+(b[i-1]+mid*i))*mid/2;
			if(tmp>=k)	
				r=mid-1;
			else	
				l=mid+1;
		}
		k-=((b[i-1]+i)+(b[i-1]+r*i))*r/2;
		//从这个数所对应的序列中查找 k 的位置  
		long long len=1;
		while(true)
		{
			long long tmp=9*pow(10,len-1)*len;
			if(tmp>=k) break;
			k=k - tmp;
			len++;
		}
		
		long long num=(k+len-1)/len;
		//减去前面num-1个len位数,在第num个数中继续搜索位置 
		k-=(num-1)*len;
		//第num个len位数等于几 
		num=pow(10,len-1)+num-1; 
		//位于第几位
		long long cnt=len-k+1;
		for(long long i=1;i<cnt;i++) 
			num/=10;
		int ans = num%10;
		printf("%d\n",ans);
	}
	return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
11086 排序问题再探讨 时间限制:1000MS 内存限制:65535K 提交次数:0 通过次数:0 题型: 编程题 语言: 无限制 Description 此题以程序填空的形式进行,请将下列程序框架复制到本机,并按下面要求填充完整后再用g++编译器提交, 在不改变程序框架情况下,可以自由添加所需的函数和变量,或修改合适的函数参数。 1,请改写一个"递归"的插入排序,排序a[0…n-1],先递归的排序a[0…n-2],然后再将a[n-1]插入到已排序的a[0…n-2]中去。 2,自然合并排序,书上2.7节最后介绍的算法,请实现它。 3,快速排序,选择"中位数"作为轴值然后进行左右段分区,请实现它。 #include #include "stdlib.h" using namespace std; const int SIZE = 10001; int a[SIZE]; void RecurInsertionSort(int p, int q) //对a[p…q]的递归插入排序,参数可根据自己需要修改。 { …… } void NaturalMergeSort(int n) //对n个元素的自然合并排序,参数可根据自己需要修改。 { …… } int Partition(int x, int p, int q) //以x为基准元素划分a[p…q],返回基准下标. 书上2.8节有。参数可根据自己需要修改。 { …… } int median(int p, int q) //挑出a[p…q]的中位数,并返回中位数,参数可根据自己需要修改。 { …… } void QuickSort(int p,int q) //参数可根据自己需要修改。 { if(p>=q)return; int x = median(p, q); int i=Partition(x,p,q); QuickSort(p,i-1); QuickSort(i+1,q);//递归 } int main() { int i,n; cin >> n; //递归插入排序 for(i=0;i> a[i]; } RecurInsertionSort(0,n-1); cout << "Insert sort: "; for(i=0;i<n;i++) { cout << a[i] << ' '; } //自然合并排序 for(i=0;i> a[i]; } NaturalMergeSort(n); cout << "\nNatural merge sort: "; for(i=0;i<n;i++) { cout << a[i] << ' '; } //快速排序 for(i=0;i> a[i]; } QuickSort(0, n-1); cout << "\nQuick sort: "; for(i=0;i<n;i++) { cout << a[i] << ' '; } return 0; } 输入格式 第一行:n,表示n个元素待排序。n不超过10000个。 第二行:n个数的序列,用来做递归的插入排序; 第三行:n个数的序列,用来做自然合并排序; 第四行:n个数的序列,用来做快速排序。 注意:第二、三、四行之间无联系,可能相同也可能不相同。 输出格式 三行,分别为三种排序算法排序后输出。 注意前面有“Insert sort:”或“Natural merge sort:”或“Quick sort:”等输出前缀,格式以上文程序为准。 输入样例 9 3 5 2 1 7 8 1 5 9 8 5 2 1 7 3 1 5 9 5 9 2 1 7 8 1 3 5 输出样例 Insert sort: 1 1 2 3 5 5 7 8 9 Natural merge sort: 1 1 2 3 5 5 7 8 9 Quick sort: 1 1 2 3 5 5 7 8 9 提示 1,递归的插入排序不难写哈。 void RecurInsertionSort(int p, int q)//递归插入,跟递归求阶乘的思想一样,前n个排好序的数组

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值