POJ 1019:Number Sequence

题目大意:

一个数字序列形式如下:1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910 1234567891011 12345678910...

输入一个数字,输出对应数字的位数。

原文参照:http://blog.csdn.net/lin375691011/article/details/17586461

解题思路:

(1) 分组,将数字序列按照如下形式分组1 12 123...  定义数组a,a中保存每个分组数字的位数。如a[1] = 1, a[2] = 2....a[10] = 11。

定义数组b,b[i]表示前i个分组的位数和。

   (2)对于输入数字n,求出n所在分组,再求出所在位置的数字。

代码:

import java.util.Scanner;

public class Main {
	
	int a[] = new int[31270];
	long  b[] = new long [31270];
	
	// 分组
	public void group() {
		a[0] = 0;
		b[0] = 0;
		int bit = 10, cnt = 1;
		for(int i = 1; i < 31270; i ++) {
			if(i < bit) {
				a[i] = a[i - 1] + cnt;
			}else {
				bit *= 10;
				cnt ++;
				a[i] = a[i - 1] + cnt;
			}
			b[i] = b[i - 1] + a[i];
		}
	}
	
	int check(int n) {
		int i = 1;
		while(b[i] < n) {
			i ++;
		}
		return i - 1;
	}

	public long[] getB() {
		return b;
	}

	public static void main(String[] args) {
		@SuppressWarnings("resource")
		Scanner in = new Scanner(System.in);
		Main main = new Main();
		main.group();
		int t = in.nextInt();
		
		while(t > 0) {
			int q = in.nextInt();
			int pos = main.check(q);
			int cnt = (int) (q - main.getB()[pos]), len = 0;
			
			int i = 1;
			for(i = 1; len < cnt; i ++) {
				len += (int)(Math.log10((double)i)) + 1;
			}
			
			System.out.println((i-1)/(int)Math.pow((double)10, len-cnt) %10 );
			t--;
		}
			
	}

}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值