11121 - Base -2
Moderator: Board moderators
why 11121 Base -2WA
can anyone tell me why WA
Code: Select all
Last edited by bishop on Fri Aug 03, 2007 8:07 am, edited 1 time in total.
-
- Learning poster
- Posts: 54
- Joined: Mon Jan 02, 2006 3:06 am
- Location: Dhaka,Bangladesh
- Contact:
check ur code for these input:
correct output will be:
best of luck TD! ![:D](./images/smilies/icon_biggrin.gif)
Code: Select all
5
8
9
-9
-11
-13
Code: Select all
11000
11001
1011
110101
110111
![:D](./images/smilies/icon_biggrin.gif)
Sanjana
-
- Learning poster
- Posts: 81
- Joined: Wed May 09, 2007 9:59 pm
- Location: (CSE,DU) Dhaka,Bangladesh
pvncad, ur code looking good,
i got AC by using the recursion algorithem.
simple comparing the numbers.
use this example:
Keep in mind this simple algerbraic formula: if a / b = c remainder d, then bc + d = a.
Thus, to expand decimal 2 in negabinary:
2 / -2 = -1 remainder 0
-1 / -2 = 1 remainder 1
1 / -2 = 0 remainder 1
Taking those remainders in reverse order we get 110. So 110 is the negabinary representation of decimal 2.
hope it helps.
i got AC by using the recursion algorithem.
simple comparing the numbers.
use this example:
Keep in mind this simple algerbraic formula: if a / b = c remainder d, then bc + d = a.
Thus, to expand decimal 2 in negabinary:
2 / -2 = -1 remainder 0
-1 / -2 = 1 remainder 1
1 / -2 = 0 remainder 1
Taking those remainders in reverse order we get 110. So 110 is the negabinary representation of decimal 2.
hope it helps.
''I want to be most laziest person in the world''
-
- New poster
- Posts: 32
- Joined: Tue Feb 13, 2007 1:31 pm
Re: 11121 - Base -2
test cases
answer
hope it helps.
Code: Select all
4
1000000000
-1000000000
1024
-1024
Code: Select all
Case #1: 1001100111011111101111000000000
Case #2: 11000101101001010100101000000000
Case #3: 10000000000
Case #4: 110000000000
11121-Base -2
11121,plz help. after hard work i code this,bt get TLE && thn RE
if anyone can tell me if i will need to change my logic,thn i will.
if any one can tell me in this logic how i recover this RE & TLE.
my code is here:
#include <stdio.h>
#include <math.h>
#define N 2000000
#include <string.h>
#include<iostream>
#include<algorithm>
using namespace std;
long long int d=-2,e=2,n,b,z,i,sum=0,c,r=0;
char a[N];
int main()
{
//d=-2;
//e=2;
scanf("%lld",&n);
getchar();
while(r<n)
{
scanf("%lld",&b);
z=b;
i=0;
if(i==0)
{
if(b%2==0)
{
a='0';
c=0;
sum+=(a-48);
i++;
}
else{
a='1';
c=1;
sum+=(a-48);
i++;
}
}
while(1)
{
if(sum==z)
break;
if(b>0)
{
b=(b-c)/d;
if(b%2!=0)
{
a='1';
c=1;
sum+=((a-48)*pow(d,i));
i++;
}
else{
a='0';
c=0;
sum+=((a-48)*pow(d,i));
i++;
}
}
if(b<0)
{
b=(b-c)/e;
if(b%2!=0)
{
a='1';
c=1;
sum+=((a-48)*pow(d,i));
i++;
}
else{
a[i]='0';
c=0;
sum+=((a[i]-48)*pow(d,i));
i++;
}
}
}
i=i-1;
printf("Case #%lld: ",r+1);
reverse(a,a+strlen(a));
printf("%s",a);
if(r<(n-1))
printf("\n");
r++;
sum=0;
}
return 0;
}
if anyone can tell me if i will need to change my logic,thn i will.
if any one can tell me in this logic how i recover this RE & TLE.
my code is here:
#include <stdio.h>
#include <math.h>
#define N 2000000
#include <string.h>
#include<iostream>
#include<algorithm>
using namespace std;
long long int d=-2,e=2,n,b,z,i,sum=0,c,r=0;
char a[N];
int main()
{
//d=-2;
//e=2;
scanf("%lld",&n);
getchar();
while(r<n)
{
scanf("%lld",&b);
z=b;
i=0;
if(i==0)
{
if(b%2==0)
{
a='0';
c=0;
sum+=(a-48);
i++;
}
else{
a='1';
c=1;
sum+=(a-48);
i++;
}
}
while(1)
{
if(sum==z)
break;
if(b>0)
{
b=(b-c)/d;
if(b%2!=0)
{
a='1';
c=1;
sum+=((a-48)*pow(d,i));
i++;
}
else{
a='0';
c=0;
sum+=((a-48)*pow(d,i));
i++;
}
}
if(b<0)
{
b=(b-c)/e;
if(b%2!=0)
{
a='1';
c=1;
sum+=((a-48)*pow(d,i));
i++;
}
else{
a[i]='0';
c=0;
sum+=((a[i]-48)*pow(d,i));
i++;
}
}
}
i=i-1;
printf("Case #%lld: ",r+1);
reverse(a,a+strlen(a));
printf("%s",a);
if(r<(n-1))
printf("\n");
r++;
sum=0;
}
return 0;
}
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 11121-Base -2
Use the existing thread. Use the code blocks.
Your program fails for the sample input and you also don't print a newline after the last case. It looks like you're not re-initializing all your variables.
Your program fails for the sample input and you also don't print a newline after the last case. It looks like you're not re-initializing all your variables.
Check input and AC output for thousands of problems on uDebug!