• 全部
  • 问答

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

LeeMaRS 2003-01-11 08:21:31
这道题看上去是非常非常简单, 但是今天竟然只有5个人过! 其它的人都是WA. 我也WA了好几次, 真是不知到错在哪里了...
--------------------------------------
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 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
flyr99 2003-02-15
我写的第一道题的C++代码,不知道对不对,第二题还没看
#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
不用说第一题了,呵呵,想A
回复
BinaryTreeEx 2003-01-27
搂住的第一题,说说我的看法。
1--9 :全部写出来共有 9 位,比如:123456789
10--99 :全部写出来共有 90*2 位,比如:101112。。。9899
。。。
这样可以有一个数列:9,90*2,900*3。。。
构造这个数列的程序很容易,求这个数列的和也很容易,不说了。
前面一项的和记为s(1)...前面n项的和记为s(n)
对于指定的第n位个数字,我们可以对他确定一个范围:
s(j-1) <= n <= s(j)

然后,第一步:n1 = n - s(j-1)
第二步:n1 % (j-1),整数部分为a1,余数部分为a2
整数a1的第a2个数字就是要的数字。
注意:1.n > 10
2.s(j-1)=n 或 n = s(j)的情况没有考虑,但是很容易对付,不说了

回复
BambooTang 2003-01-23
test data is wrong.
回复
BambooTang 2003-01-23
除非A的数据也换了:D 不过糟糕的是 程序被我format掉了 :(
回复
BambooTang 2003-01-23
n
回复
LeeMaRS 2003-01-23
嗯, I当时的确是数据错了(当时竟然还有人AC, 真是搞笑!) A现在也有人过了. Bamboo, 你的A过了没有?
回复
oknet 2003-01-20
计算一个50000

n=50000

进入最后一个条件 return( n-38889 )/5+9999 = 12221.2

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
有没有谁搞定的,发个测试文件给我,即input及output

我submit了几次都是wrong answer, 也不知道哪里有问题
回复
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
现在大家来想想A吧.
楼上的, 那时没有新题的介绍.
回复
laomai 2003-01-16
javanew:
http://oibh.ioiforum.org/uva/
有关于UVA的详细介绍
回复
Koorama 2003-01-16
我的想法:
通式:1*9 + 2*90 + 3*900 + 4*9000 +...+ n*9000...
N由以下部分组成
1-9九个1位数
10-99九十个2位数
100-999九百个3位数
1000-9999九千个4位数
10000-99999九万个5位数
...

例如:1*9 + 2*90 + 3*900 + 4*1777 = 10000 - 3
即当输入是10000时,输出数列中999以后的第1777+1个数的第三位。
也就是2778的第三位

如果要实现算法关键是找到用于加权表示N的最大权值K,
N/K向下取整得到数字X,K-1+X 的第mod(N,K)位就是所求。
回复
cvip11 2003-01-16
fgs
回复
javanew 2003-01-16
uva是什么东东?
回复
Eastunfail 2003-01-15
够BT
回复
相关推荐
发帖
数据结构与算法
创建于2007-08-27

3.2w+

社区成员

数据结构与算法相关内容讨论专区
申请成为版主
帖子事件
创建了帖子
2003-01-11 08:21
社区公告
暂无公告