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

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

Moderator: Board moderators

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

Postby Ivo Sluganovic » Tue Jul 26, 2005 9:49 pm

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.
Thanks!

Code: Select all
#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;

string bin( int x )
{
    string sol = "";
    for( ; x; x /= 2 )
        sol.push_back( ( x & 1 ) + '0' );
   
    reverse( sol.begin(), sol.end() );
    return sol;
}

int main(){
    int t; scanf( "%d", &t );
    for( int counter = 1; counter <= t; ++counter ) {
        char op; int a, b; scanf( "%X %c %X", &a, &op, &b );
        printf( "%013s %c %013s = %d\n", bin(a).c_str(), op, bin(b).c_str(), ( ( op == '+' ) ? a + b : a - b ) );
    }
    return 0;
}
Ivo Sluganovic
New poster
 
Posts: 12
Joined: Tue Sep 21, 2004 10:08 pm

Postby Ivo Sluganovic » Wed Jul 27, 2005 9:21 pm

Can anyone think of some tricky examples?
Thank you!
Ivo Sluganovic
New poster
 
Posts: 12
Joined: Tue Sep 21, 2004 10:08 pm

Postby chunyi81 » Tue Aug 09, 2005 8:18 am

Hi, I tried compiling your code using g++ 2.95(same as OJ environment) and I got a compile error. So I'm not sure how the OJ managed to compile your program.

The compile error was:
p446test.cpp: In function `class string bin(int)':
p446test.cpp:10: no matching function for call to `basic_string<char,string_char_traits<char>,__default_alloc_template<false,0> >::push_back (int)'

Anyway, there are some test data from past threads.

I have compiled them here:

Input:
20
FF + F
ABC + CDE
AA + 991
F0C + A
FFF - FFF
AA + DDD
FFF - CCD
DDA - C9C
E0A - ABB
C - 9
A + B
FFF - 123
36 + 2
0 + 0
3 - 0
23 - 1
A46 - 1A
F - 1
14 - 13
988 - FA

Output from my AC program:
000011111111 + 0000000001111 = 270
0101010111100 + 0110011011110 = 6042
0000010101010 + 0100110010001 = 2619
0111100001100 + 0000000001010 = 3862
0111111111111 - 0111111111111 = 0
0000010101010 + 0110111011101 = 3719
0111111111111 - 0110011001101 = 818
0110111011010 - 0110010011100 = 318
0111000001010 - 0101010111011 = 847
0000000001100 - 0000000001001 = 3
0000000001010 + 0000000001011 = 21
0111111111111 - 0000100100011 = 3804
0000000110110 + 0000000000010 = 56
0000000000000 + 0000000000000 = 0
0000000000011 - 0000000000000 = 3
0000000100011 - 0000000000001 = 34
0101001000110 - 0000000011010 = 2604
0000000001111 - 0000000000001 = 14
0000000010100 - 0000000010011 = 1
0100110001000 - 0000011111010 = 2190
chunyi81
A great helper
 
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Postby sluga » Tue Feb 07, 2006 12:32 pm

Thank you, I got ACC now :)

I let go of the problem long ago because I couldn't find the error.
Now I realized that my code didn't work for negative numbers.
A sta da radim
sluga
New poster
 
Posts: 20
Joined: Sun Jan 22, 2006 10:48 pm
Location: Croatia

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

Postby fR0D » Fri May 30, 2008 4:07 pm

do we hav to consider negative numbers???if yes wat will be the result of -20 + -20.
fR0D
New poster
 
Posts: 29
Joined: Mon Feb 11, 2008 5:59 am

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

Postby 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
helloneo
Guru
 
Posts: 516
Joined: Mon Jul 04, 2005 6:30 am
Location: Seoul, Korea

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

Postby 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);     
    }
}
fR0D
New poster
 
Posts: 29
Joined: Mon Feb 11, 2008 5:59 am

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

Postby anondo_iut » Sat Sep 13, 2008 11:25 am

check <math.h> will it be <cmath>??
anondo_iut
New poster
 
Posts: 3
Joined: Sun Sep 07, 2008 6:25 pm


Return to Volume IV

Who is online

Users browsing this forum: No registered users and 1 guest