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

All about problems in Volume 4. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

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

Re: 446 WA -> so easy, but... PLEASE HELP!

Post by helloneo » Sat May 31, 2008 4:27 pm

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:

Re: 446 WA -> so easy, but... PLEASE HELP!

Post by fR0D » Sat May 31, 2008 9:03 pm

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[4],h2[4],t[1];
    char con[16][5]={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
    int val[16]={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[0]);
        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[0]=='+'?'+':'-');
        printf("0");
        for (j=3;j>strlen(h2);j--)
            printf("%s",con[0]);
        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[0]=='+'?d1+d2:d1-d2);      
    }
}

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

Re: 446 WA -> so easy, but... PLEASE HELP!

Post by anondo_iut » Sat Sep 13, 2008 11:25 am

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

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

Re: 446 WA -> so easy, but... PLEASE HELP!

Post by uDebug » Mon May 26, 2014 9:21 am

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!

Find us on Facebook. Follow us on Twitter.

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

Re: 446 WA -> so easy, but... PLEASE HELP!

Post by uDebug » Mon May 26, 2014 9:27 am

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!

Find us on Facebook. Follow us on Twitter.

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

Re: 446 WA -> so easy, but... PLEASE HELP!

Post by uDebug » Mon May 26, 2014 9:31 am

Ivo Sluganovic wrote:I am trying to solve the Kibbles ``n'' Bits ``n'' Bits ``n'' Bits problem.
It seems like a very easy problem, and it is, but I keep getting WA.
I coded it several times...
Please help me if you see any mistake...
I don't know what could be wrong in such short code.
Your code doesn't match the sample input / output.
Check input and AC output for over 7,500 problems on uDebug!

Find us on Facebook. Follow us on Twitter.

Post Reply

Return to “Volume 4 (400-499)”