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

TUHIN
New poster
Posts: 2
Joined: Fri May 06, 2005 10:56 pm
Location: RUET, BANGLADESH

For challenger

Post by TUHIN »

Dear challenger,
You wrote :
char temp[13]="0000000000000";
but assigning 13 characters in a 13 byte string will cause producing
bad result. You should use temp[14] or assign 12 0's in temp[13] and
left the last one for null character.


TUHIN
_____________________________

Think simple, Be simple and Get simple solution.

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Re: For challenger

Post by sumankar »

TUHIN wrote:Dear challenger,
You wrote :
char temp[13]="0000000000000";
but assigning 13 characters in a 13 byte string will cause producing
bad result.
This is what I get from K&R:
"If the array has unknown size, the number of characters in the string, including the terminating null character, determines its size; if its size is fixed, the number of characters in the string, not counting the terminating null character, must not exceed the size of the array. "

which means that you can assign at most 13 characters in temp.

Chanllenger:
`temp' is local in scope to the function in which it is created, i.e. created on the stack frame the function call is executed in.So returning its address is fruitless, you are pointing to something that no longer exists.

Regards,
Suman.

Rocky
Experienced poster
Posts: 124
Joined: Thu Oct 14, 2004 9:05 am
Contact:

Help On 446

Post by Rocky »

If You Not Solve This Problem,Then It May Help You.
I Debug Your Code With Various Test Case And Your Program Is Say OK.
But I AM Not Sure If Following Kind Of Test Case Is Possible Then Your Program Is Not Ok.Try With This.

input:
2
000000000000A + 000000000000A
000000000000002145 + 000000000000002145

And Your Programm Is Exit For This Input.

But My Acc Programm Says The Output Is:

0000000001010 + 0000000001010 = 20
0000101000101 + 0000101000101 = 17034

GOOD LUCK
Rocky

Ivo Sluganovic
New poster
Posts: 12
Joined: Tue Sep 21, 2004 10:08 pm

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

Post by Ivo Sluganovic »

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

Post by Ivo Sluganovic »

Can anyone think of some tricky examples?
Thank you!

chunyi81
A great helper
Posts: 293
Joined: Sat Jun 21, 2003 4:19 am
Location: Singapore

Post by chunyi81 »

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

athlon19831
New poster
Posts: 20
Joined: Thu Jan 19, 2006 2:32 pm

446 Output Limit Exceeded

Post by athlon19831 »

my code :(
#include "iostream.h"
#include "stdio.h"
#include "string.h"

int main(int argc, char* argv[])
{
long N;
char h1[3],h2[3];
char b1[15],b2[15];
long val;
long l1,r1;
long i,j;
long sum1,sum2;
long sum;
char c;
while(cin>>N)
{
for(i=0;i<N;i++)
{
cin>>h1>>c>>h2;

l1=strlen(h1);
r1=strlen(h2);

sum1=0;
val=16;
for(j=0;j<l1;j++)
{
if(h1[j]>='A'&&h1[j]<='F')
{
sum1=sum1*val+int(h1[j])-55;
}
else
{
sum1=sum1*val+int(h1[j])-48;
}
}
sum2=0;
val=16;
for(j=0;j<r1;j++)
{
if(h2[j]>='A'&&h2[j]<='F')
{
sum2=sum2*val+int(h2[j])-55;
}
else
{
sum2=sum2*val+int(h2[j])-48;
}
}
switch(c)
{
case '+':sum=sum1+sum2;break;
case '-':sum=sum1-sum2;break;
}

i=12;//display left Binary number
while(sum1/2)
{
b1=char(sum1%2+48);
sum1=sum1/2;
i--;
}
b1='1';
i--;
while(i>=0)
{
b1='0';
i--;
}
for(j=0;j<13;j++)
{
cout<<b1[j];
}

cout<<" "<<c<<" ";

i=12;//display right Binary number
while(sum2/2)
{
b2=char(sum2%2+48);
sum2=sum2/2;
i--;
}
b2='1';
i--;
while(i>=0)
{
b2='0';
i--;
}
for(j=0;j<13;j++)
{
cout<<b2[j];
}
cout<<" = "<<sum<<endl;
}
}
return 0;
}

mamun
A great helper
Posts: 286
Joined: Mon Oct 03, 2005 1:54 pm
Location: Bangladesh
Contact:

Post by mamun »

Use code tagging for posting code, otherwise it looks so miserable.
Read carefully how the input is given. Don't wait for EOF to terminate your program.

sluga
New poster
Posts: 20
Joined: Sun Jan 22, 2006 10:48 pm
Location: Croatia

Post by sluga »

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

vinit_iiita
New poster
Posts: 30
Joined: Mon Jun 19, 2006 10:37 pm
Contact:

Post by vinit_iiita »


why wrong ans.. :cry: ..

Code: Select all

#include<iostream>
#include<string>
#include<vector>
#include<cmath>
#include<sstream>
using namespace std;
int power(int r,int s)
{
    int d=1;
    for(int i=0;i<s;i++)
    d=d*r;
    return d;
}
int main()
{
    
    string q[16]={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
    
    string n;
    getline(cin,n);
    for (int i=1;i<=(int)n[0]-48;i++)
    {   
      
        string s;
        getline(cin,s);  
        vector<string> v; 
        string word; 
        istringstream ins(s);
        while (ins>>word)
        {    
              v.push_back(word);
              }
        int a=0,b=0,c=0; 
        cout<<"0";
            if(v[0].size()<3)
            for (int r=0;r<4*(3-v[0].size());r++)
            cout<<0;     
        for (int j=0;j<v[0].size();j++)
        {
            
            int g=v[0].size()-j-1;
            if((int)v[0][j]>47 && (int)v[0][j]<58)
                {
                              
                                cout<<q[(int)v[0][j]-48];
                                a=a+((int)v[0][j]-48)*power(16,g);
                                }
                else if((int)v[0][j]>64 && (int)v[0][j]<71)
                {
               
                     cout<<q[(int)v[0][j]-55];
                     a=a+((int)v[0][j]-55)*power(16,g);
                     }
                     }     
              cout<<" "<<v[1]<<" ";
               cout<<"0";
            if(v[2].size()<3)
            for (int r=0;r<4*(3-v[2].size());r++)
            cout<<0;
               for (int j=0;j<v[2].size();j++)
        {   
           
            int g=v[2].size()-j-1;
            if((int)v[2][j]>47 && (int)v[2][j]<58)
                {

                                cout<<q[(int)v[2][j]-48];
                                b+=((int)v[2][j]-48)*power(16,g);
                                }
                else if((int)v[2][j]>64 && (int)v[2][j]<71)
                {
                    
                     cout<<q[(int)v[2][j]-55];
                     b+=((int)v[2][j]-55)*power(16,g);
                     }
                     }        
              cout<<" = ";
              if(v[1][0]=='+')
              cout<<a+b<<endl;
              else if(v[1][0]=='-')
              cout<<a-b<<endl;
              }
              return 0;
              }
                         
can some one find the missing test cases...[/b]
win

abhi
Learning poster
Posts: 94
Joined: Fri Nov 25, 2005 7:29 pm

446 - TLE !!!!!

Post by abhi »

hello everyone. here is my code for the problem. i get TLE :(
plz help me.

Code: Select all

# include <iostream>
# include <cctype>
# include <cstdio>
# include <string>

using namespace std;

char tab[][7]={"0000","0001","0010","0011","0100","0101","0110","0111","1000"\
,"1001","1010","1011","1100","1101","1110","1111"};

string conv(char s1[])
{
    string ret="";
    int i=0;
    while(s1[i]!='\0')
    {
        if(isdigit(s1[i]))
            ret+=tab[s1[i]-'0'];
        else
            ret+=tab[s1[i]-'0'-7];
       i++;
    }
    
    while(ret.size()!=13)
    {
        ret.insert(ret.begin(),'0');
    }
    return ret;
}

int main()
{
    char s[100],s1[10],s2[10];
    int n1,n2,sum,t;
    char x;
    
    cin>>t;
    
    while(t--)
    {
        fflush(stdin);
        gets(s);
        sscanf(s,"%x %c %x",&n1,&x,&n2);
        sum = (x == '+' ? n1+n2 :n1-n2);
        sscanf(s,"%s %*c %s",s1,s2); 
        cout<<conv(s1)<<' '<<((x == '+') ?'+':'-')<<' '<<conv(s2)<<" = ";
        cout<<sum<<'\n';
    }
    
return 0;
}

    
Last edited by abhi on Sat Nov 11, 2006 5:19 pm, edited 2 times in total.

abhi
Learning poster
Posts: 94
Joined: Fri Nov 25, 2005 7:29 pm

Post by abhi »

still TLE :( what could be wrong ?

rvarshne
New poster
Posts: 3
Joined: Tue Dec 05, 2006 11:02 pm

446 WA

Post by rvarshne »

Hi,
Can anyone tell me why this code give WA?
Thanks

Code: Select all

#include <stdio.h>
#include <string.h>

char buf1[100], buf2[100];

void reverse(char * begin, char * end)
{
              char temp;
              while(end > begin)
			  {
					temp = *end;
					*end-- = *begin;
					*begin++ = temp;
              }
}

void itoa(int value, char* str, int base)
{
      static char num[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
      char * out = str;

      if (base < 2 || base > 35)
      {
            *out = '\0';
            return;
      }

	  if(value == 0)
	  {
		  *out++ = '0';
	  }
	  else
	  {
		int count = 0;
		while(value != 0 && count < 13)
		{
		    *out++ = num[value % base];
			value = value / base;
			count++;
		}
	  }

      *out = '\0';
      reverse(str, out - 1);
}

int main()
{
      int a, b;
      char sign;
      int value;
      int numTests;
      scanf("%d", &numTests);

      for(int i = 0; i < numTests; i++)
      {
              scanf("%x %c %x", &a, &sign, &b);

              if(sign == '+')
              {
                      value = a + b;
              }
              else if(sign == '-')
              {
                      value = a - b;
              }

               if (a >= 0 && b >= 0)
               {
                      itoa(a, buf1, 2);
                      itoa(b, buf2, 2);
                      printf("%013s %c %013s = %d\n", buf1, sign, buf2, value);
               }
               else if(a < 0 && b >= 0)
               {
                      itoa(-a, buf1, 2);
                      itoa(b, buf2, 2);
                      printf("-%013s %c %013s = %d\n", buf1, sign, buf2, value);
               }
               else if(b < 0 && a >= 0)
               {
                      itoa(a, buf1, 2);
                      itoa(-b, buf2, 2);

                      if(sign == '+')
                      {
                              sign = '-';
                      }
                      else
                      {
                              sign = '+';
                      }

                      printf("%013s %c %013s = %d\n", buf1, sign, buf2, value);
               }
               else if(b < 0 && a < 0)
               {
                       itoa(-a, buf1, 2);
                       itoa(-b, buf2, 2);

                       if(sign == '+')
                       {
                              sign = '-';
                       }
                       else
                       {
                              sign = '+';
                       }
                       printf("-%013s %c %013s = %d\n", buf1, sign, buf2, value);
               }
      }

      return 0;
}

hridoy
New poster
Posts: 21
Joined: Tue May 08, 2007 10:30 am
Location: Dhaka
Contact:

acm - 446(WA)

Post by hridoy »

Can anyone please tell me why m i getting WA??

Code: Select all

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

int decimal(char s1[])
{
	int i,n,sum=0;
	n=strlen(s1);
	for(i=n-1;i>=0;i--)
	{
		if(s1[i]=='A')
			sum+=(10*pow(16,n-i-1));
		else if(s1[i]=='B')
			sum+=(11*pow(16,n-i-1));
		else if(s1[i]=='C')
			sum+=(12*pow(16,n-i-1));
		else if(s1[i]=='D')
			sum+=(13*pow(16,n-i-1));
		else if(s1[i]=='E')
			sum+=(14*pow(16,n-i-1));
		else if(s1[i]=='F')
			sum+=(15*pow(16,n-i-1));
		else
			sum+=((s1[i]-48)*pow(16,n-i-1));
	}
	return sum;
}

void print(unsigned n)
{
	unsigned i,m=1;
	//printf("0");
	m=1<<12;
	for(i=1;i<=13;i++)
	{
		if(n&m)
			printf("1");
		else
			printf("0");
		m>>=1;
	}
}

main()
{
	unsigned i,j,n,i1,i2,sum;
	char s1[3],s2[3],c;
	scanf("%u",&n);
	for(i=1;i<=n;i++)
	{
		scanf("%s %c %s",s1,&c,s2);
		i1=decimal(s1);
		i2=decimal(s2);
		if(c=='+')
			sum=i1+i2;
		else
			sum=i1-i2;
		print(i1);
		printf(" %c ",c);
		print(i2);
		printf(" = %u",sum);
		printf("\n");
	} 
}

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 »

do we hav to consider negative numbers???if yes wat will be the result of -20 + -20.

Post Reply

Return to “Volume 4 (400-499)”