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

同意楼上的思路,给出Java源码

Posted by goslingL at 2010-07-29 13:17:38 on Problem 1019 and last updated at 2010-07-29 13:23:11
In Reply To:O(1)终于搞出来了.. Posted by:Viaxl at 2010-03-18 03:14:20
public class Main {
	
	//  求数 n 的位数
	public static int getWeiShu(int n)
	{
		int weiShu = 0;
		while(n != 0)
		{
			n = n / 10;
			weiShu++;
		}
		
		return weiShu;
	}
	
	public static int location(int i)  
	{
		//i - 所求序数 
		//ans - 所求位的数字
		//j - 递推定位串
		//base - 记录每个串递增的位数
		//sum - 记录串的总位数 、 用作计数
		
		int ans;  
		int j = 1, base, sum = 1;
		while(i >= sum)
		{
			i -= sum;  
			j++;    //j 记载 下一个串 到哪个数字串了 -- 12...j
                        //将整个数字串中,每一个1...j视为一个单独处理的串
			base = getWeiShu(j);  
                                 //该串比上一个串多的字符数 -  j 的位数
			sum += base;        //该串的字符总数
		}
		//出口:定位到单独的串,此时有i >= 0
		if( i == 0)  //所求为该串最后一个数的最后一位
		{
			ans = (j -1) % 10;
			return ans;
		}
		
		sum = 1;  //从串(1...j) 中第一个数开始找
		base = 1;  //串中第一个数的位数是 1
		while( i >= base)  //求 1...j 串中第 i 个数字
		{
			i -= base;
			sum++;
			base = getWeiShu(sum);  //sum 的位数
		}
                //出口:定位到串中的数,此时有i >= 0
		if( i == 0)  //所求为该数的最后一位
		{
			ans = (sum -1) % 10;
		    return ans;
	    }
		
		j = getWeiShu(sum) - i;
		while( j-- > 0)  //ans is the ith number of (sum) 
		{
			sum /= 10;
		}
		ans = sum % 10;
		return ans;
	}
	public  static void main(String[] args)
	{
		
		java.util.Scanner input = new java.util.Scanner(System.in);
		int t = input.nextInt();  //test number
		
		while(t-- > 0)
		{
			int i = input.nextInt();
                        System.out.println( location(i) );
		}
	}

}

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