Page 3 of 4

For challenger

Posted: Sat May 21, 2005 8:35 pm
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.

Re: For challenger

Posted: Mon May 23, 2005 6:06 am
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.

Help On 446

Posted: Tue May 31, 2005 8:41 am
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

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

Posted: Tue Jul 26, 2005 9:49 pm
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;
}

Posted: Wed Jul 27, 2005 9:21 pm
by Ivo Sluganovic
Can anyone think of some tricky examples?
Thank you!

Posted: Tue Aug 09, 2005 8:18 am
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

446 Output Limit Exceeded

Posted: Tue Jan 31, 2006 5:07 pm
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;
}

Posted: Tue Jan 31, 2006 6:19 pm
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.

Posted: Tue Feb 07, 2006 12:32 pm
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.

Posted: Mon Aug 07, 2006 1:31 pm
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]

446 - TLE !!!!!

Posted: Wed Nov 08, 2006 8:02 pm
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;
}

    

Posted: Fri Nov 10, 2006 5:54 am
by abhi
still TLE :( what could be wrong ?

446 WA

Posted: Tue Dec 12, 2006 10:55 pm
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;
}

acm - 446(WA)

Posted: Tue Jan 15, 2008 8:02 pm
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");
	} 
}

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

Posted: Fri May 30, 2008 4:07 pm
by fR0D
do we hav to consider negative numbers???if yes wat will be the result of -20 + -20.