• 全部
• 问答

# UVA CONTEST 今天第二变态的题: I - Naughty Sleepy Boys

LeeMaRS 2003-01-11 08:21:31

--------------------------------------
Problem I
Naughty Sleepy Boys

Input: standard input
Output: standard output
Time Limit: 2 seconds

Hasan and Tanveer are two naughty boys of the class. They spent most of their class time playing `Tic Tac Toe' whenever they get a chance to sit in back bench. But their teacher doesn't think that playing cross and naught is a way of making a class enjoyable. So, whenever she sees them playing she brings them to the front seat and makes them hear what she says. And you know listening to the teacher is not as enjoyable as playing `Tic Tac Toe'. So, within a couple of minutes, their eyes become dizzy and they fall into sleep resting their heads on the table. The teacher notices that and exclaims, ``You two naughty boys! Come over here.'' and she gives them some problem to solve which is lengthy and cumbersome. They try all the time not to get caught by the teacher while playing, or, for the least, not to get sleepy. But, pity for them, they hardly escapes the eagle eye of the teacher. Now, they are again caught red handed, brought to front bench, and as you know the usual case, they become sleepy. (poor Hasan and Tanveer! Couldn't the teacher be a little bit sympathetic to them?) This time the teacher is very angry and gives them a problem which is really too much for them. She asks them if she writes down the numbers from 1 to 1000 one after another what will be the 1000-th digit. You see what a tough problem for two little boys. Alam and Dalim start thinking that if they write down all the numbers like 1234567891011121314 ... ... it will take many hours for them. Moreover, the answer will possibly be incorrect because they can easily lose track of the numbers written. So, they are trying to find out a way to fool the teacher with the right answer. Can you find a way out for them?

Input

The input will contain one or more line each containing a positive integer N (N<100000000). Total line of the input file will not exceed 11000.

Output

For every N, you should calculate the N-th digit of the number 123456789101112 ... ... and print it on a line itself.

Sample Input

3
9
10
11
10000
50000

Sample Output

3
9
1
0
7
1
...全文
42 点赞 收藏 35

35 条回复

flyr99 2003-02-15

#include<iostream>
#include<math.h>
using namespace std;

//get digit in inverse order
int getDigit(int number, int n = 1)
{
int digits[10]={0};
int i=0;

for(; number != 0; i++)
{
digits[i] = number%10;
number /= 10;
}

return digits[(i++) - n];
}

static bool flag=true;
int naughtyBoy(int N, int n)
{
if(N <= 9 && flag==true)
{
return N;
flag = false;
}
if( (N - 9*pow(10, n-1)*n) > 9*pow(10, n)*(n+1) )
return naughtyBoy(N - 9*pow(10, n-1)*n, n+1);
else
{
N = N - 9*pow(10, n-1)*n;
n++;
int number = pow(10, n-1)-1 + N/n;
if(N%n != 0)
return getDigit(number+1, N%n );
else
return getDigit(number, n);
}
}

int main()
{
int N=50000;
cout << naughtyBoy(N, 1) << endl;

return 0;
}

BambooTang 2003-01-28
faint!

LeeMaRS 2003-01-27

BinaryTreeEx 2003-01-27

1--9 ：全部写出来共有 9 位，比如：123456789
10--99 ：全部写出来共有 90*2 位，比如：101112。。。9899
。。。

s(j-1) <= n <= s(j)

2.s(j-1)=n 或 n = s(j)的情况没有考虑，但是很容易对付，不说了

BambooTang 2003-01-23
test data is wrong.

BambooTang 2003-01-23

BambooTang 2003-01-23
n

LeeMaRS 2003-01-23

oknet 2003-01-20

n=50000

char str_result[10];
int n;
float number;

number = 12221.2;

if( (float)(number) != (int)(number) )
{
sprintf(str_result, "%d", (int)( number ) +1);
/* str_result="12222"; */
n= ((float)(number)-(int)(number)) * strlen(str_result);
}
else
{
sprintf(str_result, "%d", (int)( number ));
/* str_result=12221 */
n = strlen(str_result);
}
return( str_result[n-1] );

oknet 2003-01-20

n<=9 9 return n;
n<=(99-9)*2+9 189 return (n-9)/2+9
n<=(999-99)*3+(99-9)*2+9 2889 return (n-189)/3+99
n<=(9999-999)*4+(999-99)*3+(99-9)*2+9 38889 return (n-2889)/4+999
n<=(99999-9999)*5+(9999-999)*4+(999-99)*3+(99-9)*2+9 488889 return (n-38889)/5+9999

show 2003-01-17

10^n-1个数有s[n]=n*10^n-(10^n-1)/9个数

jfguo 2003-01-17
Problem A Sum-up the Primes

bryanzk 2003-01-17

create procedure calculate_array (@scope int, @digit int)
as

declare @i as integer
declare @result as varchar(1)
declare @str as varchar(6)
declare @length as integer

set @i = 1
set @length = 0

while (@i <= @scope)
begin
if @i > @scope
print 'Error, Scope exceed!!'
else
begin
set @str = cast(@i as varchar(6))
set @length = len(@str)+@length
if @length < @digit
begin
print 'the length now is '+ cast(@length as varchar(4))
set @i = @i + 1
continue
end
else
--@length >= @digit即累计长度之和超过或等于所要查询的数字所在位置
begin
set @length = @length - @digit
set @result = cast(substring ( @str , len(@str)-length,1) as int)
print 'gotcha last'
print @result
insert into result(scope, digit, result) values(@scope, @digit, @result)
break
end
end
end

go

bryanzk 2003-01-17
sqlserver 存储过程解决方案：

create procedure calculate_array (@scope int, @digit int)
as

declare @i as integer
declare @result as varchar(1)
declare @str as varchar(6)
declare @length as integer

set @i = 1
set @length = 0

while (@i <= @scope)
begin
if @i > @scope
print 'Error, Scope exceed!!'
else
begin
set @str = cast(@i as varchar(6))

set @length = len(@str)+@length

if @length < @digit

begin

print 'the length now is '+ cast(@length as varchar(4))
set @i = @i + 1
print 'still small'
print @i
continue
end
else
--@length >= @digit 即累计长度之和已经超过或等于所要查询的数字所在位置 */
begin
set @length = @length - @digit
set @result = cast(substring ( @str , len(@str)-@length,1) as int)
print 'gotcha last'
print @result
insert into result(scope, digit, result) values(@scope, @digit, @result)
break
end
end
end

go

LeeMaRS 2003-01-16
I不难, 我当时就解决了:P

laomai 2003-01-16
javanew:
http://oibh.ioiforum.org/uva/

Koorama 2003-01-16

N由以下部分组成
1-9九个1位数
10-99九十个2位数
100-999九百个3位数
1000-9999九千个4位数
10000-99999九万个5位数
...

N/K向下取整得到数字X，K-1+X 的第mod(N,K)位就是所求。

cvip11 2003-01-16
fgs

javanew 2003-01-16
uva是什么东东？

Eastunfail 2003-01-15

3.2w+

2003-01-11 08:21