## 11121 - Base -2

Moderator: Board moderators

lame
New poster
Posts: 1
Joined: Sun Oct 15, 2006 2:14 pm

### 11121 - Base -2

Hello , please give me a hint . I know that negative binary numbers are the binary number + a number 1 at the beginning , but it is not in this case. I suppose it's an easy problem , because noone didn't ask about it in the forum , but I just can't get it. It's embarassing not to see the easy problems sometimes but I still want to know the solution.

vinay
Experienced poster
Posts: 104
Joined: Thu Apr 13, 2006 2:26 pm
Location: India
Contact:
what if the number positive or negative is odd... what will be there at last place ...

after that how can we apply this process recursively..

think about it and solution is in front of u ..
If I will myself do hashing, then who will do coding !!!

mrahman
New poster
Posts: 25
Joined: Mon Oct 24, 2005 9:45 am
Contact:
Hello Lame,

Think about Simply Binary number System

Code: Select all

``````           16  8  4  2  1
6 =            1  1  0

and negative 2 (-2) base System

16  -8  4  -2  1
6 =      1   1  0   1  0``````
good luck!!
Practice Makes a man perfect

sclo
Guru
Posts: 519
Joined: Mon Jan 23, 2006 10:45 pm
Contact:
Write down a base -2 expansion for an arbitrary number, then consider it mod 2.

Jared
New poster
Posts: 3
Joined: Wed Jul 20, 2005 11:50 am
I still don't understand. Can somebody give me some more hints? Thanks a lot.

sakhassan
Experienced poster
Posts: 105
Joined: Sat Mar 11, 2006 9:42 am
Location: cse,DU
a number with base b can be expanded as follows :
n = m/b + r, where n= original number, r = reminder and m= what u need to find recursively .....
Time that gone is gone forever ...

Jared
New poster
Posts: 3
Joined: Wed Jul 20, 2005 11:50 am
sakhassan wrote:a number with base b can be expanded as follows :
n = m/b + r, where n= original number, r = reminder and m= what u need to find recursively .....
Thanks for your hint! I got AC now!

algoJo
New poster
Posts: 37
Joined: Sun Dec 17, 2006 9:02 am

### it seems correct but judges said CE..?

Code: Select all

``````Removed after AC-ed
Thanks to helloneo..
``````
Last edited by algoJo on Wed Dec 20, 2006 7:29 am, edited 1 time in total.

helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea
I got this message..

Code: Select all

``array subscript is not an integer``
So try to change the type of all 'i' to "int"

algoJo
New poster
Posts: 37
Joined: Sun Dec 17, 2006 9:02 am
wow... Thanks a lot helloneo
finally AC-ed

New poster
Posts: 27
Joined: Sun Feb 18, 2007 2:14 pm
I am using following functions to find the answer.

Can someone help me whats wrong with this code ?

Code: Select all

``deleted``
Last edited by pvncad on Thu Mar 08, 2007 3:42 pm, edited 1 time in total.

algoJo
New poster
Posts: 37
Joined: Sun Dec 17, 2006 9:02 am
try to change your data type from

Code: Select all

``````long long n
``````
to

Code: Select all

``````long double n
``````
and don't forget to remove your code after accepted...

New poster
Posts: 27
Joined: Sun Feb 18, 2007 2:14 pm
Thanks. But now it is dying with signal 11. Though, it is running file for my test cases.
please give some inputs or let me know problem with following code.

Code: Select all

``````#include<stdio.h>
#include <math.h>

void print_binary(long double n)
{
long double n1 = n / -2;
int r;
n = ceill(n1);
if (n == n1) {
r = 0;
}
else {
r = 1;
}
if (n) {
print_binary(n);
}
printf("%d", r);
}

int main()
{
int N, count;
long double n;
count = 1;
scanf("%d", &N);
while (N --) {
scanf("%Lf", &n);
printf("Case #%d: ", count);
print_binary(n);
printf("\n");
count ++;
}
return 0;
}``````
thanks

helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea
Give EPS when you compare floating point numbers..
Try this..

Code: Select all

``````  if (n < 0.000001 && n > -0.000001) {
print_binary(n);
} ``````
Now you will get WA..

lovemagic
Learning poster
Posts: 52
Joined: Thu Oct 02, 2003 11:38 am
can somebody tell me where's my wrong?

Code: Select all

``````
code removed...

``````
Last edited by lovemagic on Tue Jun 19, 2007 11:45 pm, edited 1 time in total.
khobaib