Page 1 of 1

11634 - Generate random numbers

Posted: Wed Aug 05, 2009 9:24 pm
by sanjoy_sust
thanks saif ........................accepted

Re: 11634 - Generate random numbers

Posted: Sat Aug 08, 2009 8:09 pm
by saiful_sust
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

Re: 11634 - Generate random numbers

Posted: Sun Nov 30, 2014 2:30 pm
by Shahidul.CSE
I am getting WA. Whats wrong with my code?

Code: Select all

Accepted !! 

Re: 11634 - Generate random numbers

Posted: Sun Nov 30, 2014 3:34 pm
by lighted
Input

Code: Select all

1
2
10
11
999
1000
0
Acc Output

Code: Select all

2
2
3
3
23
2

Re: 11634 - Generate random numbers

Posted: Sun Nov 30, 2014 3:44 pm
by lighted
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. :)

Re: 11634 - Generate random numbers

Posted: Sun Nov 30, 2014 4:08 pm
by Shahidul.CSE
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. :)