11634 - Generate random numbers

All about problems in Volume 116. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

Post Reply
sanjoy_sust
New poster
Posts: 2
Joined: Wed Jul 22, 2009 9:10 pm

11634 - Generate random numbers

Post by sanjoy_sust » Wed Aug 05, 2009 9:24 pm

thanks saif ........................accepted
Last edited by sanjoy_sust on Sun Aug 09, 2009 9:33 am, edited 1 time in total.

saiful_sust
Learning poster
Posts: 97
Joined: Fri Aug 22, 2008 10:18 pm
Location: CSE.SUST.SYLHET

Re: 11634 - Generate random numbers

Post by saiful_sust » Sat Aug 08, 2009 8:09 pm

Hi sonjoy .............. :) :) :)

ur getting wa becoz...ur code gove wa out put for
this
input:

Code: Select all

100
0
output:

Code: Select all

1

Shahidul.CSE
Experienced poster
Posts: 148
Joined: Sun Jul 13, 2014 4:32 am
Location: Rangpur, Bangladesh

Re: 11634 - Generate random numbers

Post by Shahidul.CSE » Sun Nov 30, 2014 2:30 pm

I am getting WA. Whats wrong with my code?

Code: Select all

Accepted !! 
Last edited by Shahidul.CSE on Sun Nov 30, 2014 4:09 pm, edited 1 time in total.
Md. Shahidul Islam
Dept. of CSE at Begum Rokeya University, Rangpur, Bangladesh
UVa id: http://uhunt.felix-halim.net/id/438420
My facebook account,
Email me: shahidul.cse.brur@gmail.com

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 11634 - Generate random numbers

Post by lighted » Sun Nov 30, 2014 3:34 pm

Input

Code: Select all

1
2
10
11
999
1000
0
Acc Output

Code: Select all

2
2
3
3
23
2
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

lighted
Guru
Posts: 585
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 11634 - Generate random numbers

Post by lighted » Sun Nov 30, 2014 3:44 pm

You could simplify your code. You can convert a number using sprintf to get 4 middle digits.

Code: Select all

sprintf(s, "%08d", n * n);

for (n = 0, i = 2; i < 6; i++) n = n * 10 + s[i] - '0';
I use array used[10000] (initially set to 0) to mark previous numbers.

Code: Select all

used[n] = 1
Now you can check in O(1) if a number was already used. :)
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman

Shahidul.CSE
Experienced poster
Posts: 148
Joined: Sun Jul 13, 2014 4:32 am
Location: Rangpur, Bangladesh

Re: 11634 - Generate random numbers

Post by Shahidul.CSE » Sun Nov 30, 2014 4:08 pm

Thanks lighted! Got AC now!! :D
My mistake was not considering 0 as a new random number. Before counting it, I stopped, which was wrong. :p
I am sending you my modified code in PM. :)
Md. Shahidul Islam
Dept. of CSE at Begum Rokeya University, Rangpur, Bangladesh
UVa id: http://uhunt.felix-halim.net/id/438420
My facebook account,
Email me: shahidul.cse.brur@gmail.com

Post Reply

Return to “Volume 116 (11600-11699)”