### 11634 - Generate random numbers

Posted:

**Wed Aug 05, 2009 9:24 pm**thanks saif ........................accepted

The Online Judge board

https://uva.onlinejudge.org/board/

https://uva.onlinejudge.org/board/viewtopic.php?f=51&t=42942

Page **1** of **1**

Posted: **Wed Aug 05, 2009 9:24 pm**

thanks saif ........................accepted

Posted: **Sat Aug 08, 2009 8:09 pm**

input:Hi sonjoy ..............

ur getting wa becoz...ur code gove wa out put for

this

Code: Select all

```
100
0
```

Code: Select all

```
1
```

Posted: **Sun Nov 30, 2014 2:30 pm**

I am getting WA. Whats wrong with my code?

Code: Select all

`Accepted !! `

Posted: **Sun Nov 30, 2014 3:34 pm**

Input
Acc Output

Code: Select all

```
1
2
10
11
999
1000
0
```

Code: Select all

```
2
2
3
3
23
2
```

Posted: **Sun Nov 30, 2014 3:44 pm**

You could simplify your code. You can convert a number using sprintf to get 4 middle digits.
I use array used[10000] (initially set to 0) to mark previous numbers.
Now you can check in O(1) if a number was already used.

Code: Select all

```
sprintf(s, "%08d", n * n);
for (n = 0, i = 2; i < 6; i++) n = n * 10 + s[i] - '0';
```

Code: Select all

`used[n] = 1`

Posted: **Sun Nov 30, 2014 4:08 pm**

Thanks lighted! Got AC now!!

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.

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.