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 xjtuzxh at 2014-09-08 17:18:41 on Problem 1019 and last updated at 2014-09-08 17:31:16
import java.io.*;
import java.util.*;
import java.math.*;

public class Main1019{
	public static Scanner cin=new Scanner(System.in);
	public static PrintWriter cout=new PrintWriter(System.out,true);
	public static ArrayList<Integer> result=new ArrayList<Integer>();
	public static void main(String[]args)
	{
		int t;
		int i;
 		t=cin.nextInt();
		while(t>0)
		{
			i=cin.nextInt();
			int k=1;
			long sum=0;
			int m; 
			while(sum<i)
			{
				sum+=compute(k);
				k++;
			}
			m=(int)((long)i-sum+(long)compute(k-1));
			result.add(find(k-1,m));
			t--;
		}
		for(int r:result)
		{
			cout.println(r);
		}
	}
	public static int calbit(int w)
	{	
		int bit=1;
		while(w/10!=0)
		{
			w/=10;
			bit++;
		}
		return bit;
	}
	public static int find(int x,int m)
	{	
		for(int l=1;l<=x;l++)
		{
			m=m-calbit(l);
			if(m<=0)
			{
				m+=calbit(l);
				return (int)(l/Math.pow(10,calbit(l)-m))%10; 
			}
		}
		return 0;
	}
	public static int compute(int k)
	{
		int bit=calbit(k);
		int sum=0;
		for(int q=1;q<bit;q++)
		{
			sum+=9*Math.pow(10,q-1)*q;
		}
		sum+=(k-Math.pow(10,bit-1)+1)*bit;
		return sum;
	}
}

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