## 446 - Kibbles "n" Bits "n" Bits "n" Bits

Moderator: Board moderators

helloneo
Guru
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

fR0D wrote:do we hav to consider negative numbers???if yes wat will be the result of -20 + -20.
Well.. I'm not sure if we have to consider negative numbers..
But my output for the input "-20 + -20" is..

1111111100000 + 1111111100000 = -64

fR0D
New poster
Posts: 29
Joined: Mon Feb 11, 2008 5:59 am
Contact:

can u pls point wats wrong with my program???
it gives run time error.

Code: Select all

``````#include<iostream>
using namespace std;
#include<math.h>

int main()
{
char h1,h2,t;
char con={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
int val={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15},d1,d2,N,i,j,k,l1,l2;
scanf("%d",&N);
for (i=0;i<N;i++)
{
d1=0,d2=0;
scanf("%s%s%s",&h1,&t,&h2);
printf("0");
for (j=3;j>strlen(h1);j--)
printf("%s",con);
l1=strlen(h1);
for (k=0;k<l1;k++,j--)
{
if (isdigit(h1[k]))
{
printf("%s",con[h1[k]-48]);
d1+=(int)pow(16,j-1)*val[h1[k]-48];
}
else
{
printf("%s",con[h1[k]-55]);
d1+=(int)pow(16,j-1)*val[h1[k]-55];
}
}
printf(" %c ",t=='+'?'+':'-');
printf("0");
for (j=3;j>strlen(h2);j--)
printf("%s",con);
l2=strlen(h2);
for (k=0;k<l2;k++,j--)
{
if (isdigit(h2[k]))
{
printf("%s",con[h2[k]-48]);
d2+=(int)pow(16,j-1)*val[h2[k]-48];
}
else
{
printf("%s",con[h2[k]-55]);
d2+=(int)pow(16,j-1)*val[h2[k]-55];
}
}
printf(" = %d\n",t=='+'?d1+d2:d1-d2);
}
}
``````

anondo_iut
New poster
Posts: 3
Joined: Sun Sep 07, 2008 6:25 pm

check <math.h> will it be <cmath>??

uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

fR0D wrote:do we hav to consider negative numbers???
Nope. My AC program didn't. There's subtraction of two positive integers but nothing like

Code: Select all

``-FA + -40``
in the input.

The test cases by chunyi81 are great.
Last edited by uDebug on Mon May 26, 2014 9:29 am, edited 1 time in total.
Check input and AC output for over 7,500 problems on uDebug!

uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm

fR0D wrote:can u pls point wats wrong with my program???
it gives run time error.
(1) Change

Code: Select all

``scanf("%s%s%s",&h1,&t,&h2);``
to

Code: Select all

``scanf("%s%s%s",h1,t,h2);``
(2) Remember that even though "t" is just a one character string, you need to allocate enough space for the null character as well.

(3) The code doesn't have the header file for "strlen".
Check input and AC output for over 7,500 problems on uDebug!

uDebug
A great helper
Posts: 475
Joined: Tue Jul 24, 2012 4:23 pm