## 11634 - Generate random numbers

Moderator: Board moderators

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

### 11634 - Generate random numbers

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

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

### Re: 11634 - Generate random numbers

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
Email me: shahidul.cse.brur@gmail.com

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

### Re: 11634 - Generate random numbers

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: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

### Re: 11634 - Generate random numbers

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