Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

一次AC~~贴个二分代码~~高手指教

Posted by Sinit at 2016-12-25 14:00:03 on Problem 1019
#include<cstdio>
#include<algorithm>
using namespace std;
unsigned group[40000], sum[40000];//题目中给的最大数据一定不会超过40000组(粗略) 
int main()
{
	int t, i, j, k, n, ans; 
	char s[10];
	scanf("%d", &t);
	group[0] = 0, sum[0] = 0;
	for( j=1; j<40000; j++)
	{
		group[j] += group[j-1] + 1 + (j/10 > 0) + (j/100 > 0) + (j/1000 > 0) + (j/10000 > 0);//递推计算第j组里有几个数字 
		sum[j] = sum[j-1] + group[j];//计算前j组数字个数之和 
	}
	while( t-- )
	{
		scanf("%d", &i);
		k = lower_bound(sum, sum+40000, i) - sum;//计算第i个数位于哪个组 
		n = i - sum[k-1];//计算该数位于该组中的第几个位置 
		k = lower_bound(group, group+40000, n) - group;//找到位于哪个数中 
		n -= group[k-1];//该数的第几个位置 
		sprintf(s, "%d", k);//转化为字符形式,方便查找 
	 	ans = s[n-1] - '0';
	 	printf("%d\n", ans);
	}
	return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator