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

User avatar
sohel
Guru
Posts: 856
Joined: Thu Jan 30, 2003 5:50 am
Location: New York

446 WA

Post by sohel » Sat Feb 08, 2003 2:39 pm

WA code


[code]
#include<stdio.h>
#include<string.h>
#include<math.h>
int main()
{
char a[4];
char c[4];
char sa[15];
char sc[15];
char s;
int da=0,dc=0,n,i,la,lc,cnt,k,p;
scanf("%d",&n);
for(cnt=0;cnt<n;cnt++)
{ scanf("%s %c %s",a,&s,c);
la=strlen(a);
lc=strlen(c);
for(i=la-1;i>=0;i--)
{ if(a[i]>='0' && a[i]<='9')
da=da+pow(16,la-i-1)*(a[i]-'0');
else
da=da+pow(16,la-i-1)*(a[i]-'A'+10);
}
for(i=lc-1;i>=0;i--)
{ if(c[i]>='0' && c[i]<='9')
dc=dc+pow(16,lc-i-1)*(c[i]-'0');
else
dc=dc+pow(16,lc-i-1)*(c[i]-'A'+10);
}

for(k=1,p=12;k<8193;k=k*2,p--)
{ if(da&k) sa[p]='1';
else sa[p]='0';
if(dc&k) sc[p]='1';
else sc[p]='0';
}
sa[13]='\0';
sc[13]='\0';
if(s=='+') printf("%s + %s = %d\n",sa,sc,da+dc);
if(s=='-') printf("%s - %s = %d\n",sa,sc,da-dc);
da=0;
dc=0;
}
return 0;
}
[/code]
Last edited by sohel on Tue Jan 15, 2008 9:36 pm, edited 3 times in total.

Red Scorpion
Experienced poster
Posts: 192
Joined: Sat Nov 30, 2002 5:14 am

Post by Red Scorpion » Sat Feb 15, 2003 8:47 am

Sorry I don't read your code,
but try this test cases:
sample input:
10
FF + F
ABC + CDE
AA + 991
F0C + A
FFF - FFF
AA + DDD
FFF - CCD
DDA - C9C
E0A - ABB
C - 9

sample output:
0000011111111 + 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

Hope this helps. :lol:

Jalal
Learning poster
Posts: 65
Joined: Sun Jun 02, 2002 8:41 pm
Location: BANGLADESH
Contact:

446---------compiler error

Post by Jalal » Fri May 16, 2003 10:31 pm

Plz give me just one reason for satisfing with compiler error!
not prob of submiting i submits with acm site......... :cry: :x

/* @JUDGE_ID: XXXXX 446 C++ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void bin13(char x[], int l)
{
int i,j;
if(l==1)
printf("000000000");
else if(l==2)
printf("00000");
else if(l==3)
printf("0");

char bin[16][20]={"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
char hd[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
for(i=0; i<l; i++)
{
for(j=0; j<16; j++)
if((hd[j]== x))
break;
printf("%s", bin[j]);
}

}
int main()
{
int i, N;
scanf("%d", &N);


for(i=0; i<N; i++)
{
int d, h1, h2;
char x, c1[20], c2[20];
int l1, l2;

scanf("%x %c %x", &h1, &x, &h2);

itoa(h1,c1,16);
l1 = strlen(c1);
bin13(c1, l1);
if(x=='+')
{
printf(" + ");
d = h1 + h2;
}
else if(x=='-')
{
printf(" - ");
d = h1 - h2;
}
itoa(h2,c2,16);
l2 = strlen(c2);
bin13(c2,l2);
printf(" = %d\n", d);
}
return 0;
}


/* @END_OF_SOURCE_CODE */

LittleJohn
Learning poster
Posts: 83
Joined: Wed Feb 27, 2002 2:00 am
Location: Taiwan

Post by LittleJohn » Sat May 17, 2003 5:34 am

Hi, I use Linux too. The error message is as follows:

Code: Select all

/tmp/cc0zO5PY.o(.text+0x1ad): In function `main':
: undefined reference to `itoa'
/tmp/cc0zO5PY.o(.text+0x229): In function `main':
: undefined reference to `itoa'
collect2: ld returned 1 exit status
I think the problem is "itoa", maybe you shouldn't use that.. :wink:

Jalal
Learning poster
Posts: 65
Joined: Sun Jun 02, 2002 8:41 pm
Location: BANGLADESH
Contact:

Post by Jalal » Sun May 18, 2003 2:07 am

Its cant be fair :(
If i can use atoi then why i wont allow for itoa.......... :x
any way wt to do.....try with another alg.......

Sayutee
New poster
Posts: 9
Joined: Wed Jun 04, 2003 7:50 am
Location: Bangladesh

Post by Sayutee » Fri Jun 06, 2003 7:28 am

I think the same as "Little John". the problem is itoa(). It is not supported in Linux/Unix.

Besides, you have to print exactly 13 digits for each binary number.

It will be better if you compile your programme usibg unix/linux before submission.

with best wishes

User avatar
Riyad
Experienced poster
Posts: 131
Joined: Thu Aug 14, 2003 10:23 pm
Location: BUET
Contact:

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

Post by Riyad » Wed Oct 01, 2003 6:25 pm

why am i getting tle in 446 . here is my code . pliz help

Code: Select all

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

#define YES 1
#define NO 0

int htoi(char s[]){

	int hexdigit,i,inhex,n;
	
	i=0;
	n=0;
	inhex=YES;
	
	for(;inhex==YES;++i){
	
		if(s[i]>='0' && s[i]<='9')
			hexdigit=s[i]-'0';
		
		else if(s[i]>='a' && s[i]<='f')
			hexdigit=s[i]-'a'+10;
		
		else if(s[i]>='A' && s[i]<='F')
			hexdigit=s[i]-'A'+10;
		
		else 
			inhex=NO;
		
		if(inhex==YES)
			
			n=16*n+hexdigit;
	
	}

	return n;



}

void show_answer(int answer[], int counter)
{
	register int n;
	
	for( n=counter; n>=0; n--)
	{
		printf("%d",answer[n]);
	}
}

void convert_DtoB(int decimal)
{
	int	remainder = 0;

	int	counter = 0;
	int	binary[16] = {0}; 

	while( decimal != 1 )
	{
		remainder = decimal % 2;	
		decimal = (int)(decimal / 2);	

		binary[counter] = remainder;	
		counter++;
	}
	binary[counter] = decimal;	

	show_answer(binary,12);	
}





int main(){
	
	int count;
	long int result;
	char no1[50],no2[50];
	char optr[5];

	int No1,No2;
	
	/*freopen("input.in","rt",stdin);*/
	
	scanf("%d",&count);
	
	while(count>0){
	
		scanf("%s %s %s",&no1,&optr,&no2);
		
		No1=htoi(no1);
		No2=htoi(no2);

		convert_DtoB(abs(No1));
		
		printf(" %s ",optr);
		
		convert_DtoB(abs(No2));
		
		if(strcmp(optr,"+")==0){
			
			result=No1+No2;
		}

		else {
		
			result=No1-No2;
		}

		printf(" = %ld\n",result);
	
		count--;
	
	}

	return 0;




}
Riyad
HOLD ME NOW ,, I AM 6 FEET FROM THE EDGE AND I AM THINKIN.. MAY BE SIX FEET IS SO FAR DOWN

osan
New poster
Posts: 47
Joined: Tue Jul 29, 2003 12:03 pm
Location: Bangladesh,Dhaka.
Contact:

446: Change your algorithm

Post by osan » Thu Oct 16, 2003 1:58 pm

if u don't mind, pls change ur algorithm....
i think there is more shortcut than ur algorithm.

Get input like

scanf("%x %c %x", &a,&c, &b);


then convert the HEXADECIMAL number to BINARY

There is a very easy way to convert HEXADECIMAL->>DECIMAL->> BINARY

i think it will be more easier. Think it in easy way.

Good Luck!!!


:)

ongkon
New poster
Posts: 2
Joined: Sat Oct 25, 2003 7:18 pm
Location: bangladesh

446: why WA.......... plz help me

Post by ongkon » Tue Oct 28, 2003 5:02 am

I got wrong answer in this problem, but i cannt find the reason(s).Here is my code in C:

#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
#define MAX 13

void main()
{
char arr1[5],arr2[5],ch;
long num1[MAX],num2[MAX],n,a,b,c,d,i,j,k,l,p,q;

/*freopen("g:\in1.txt","rt",stdin);*/

scanf("%ld",&n);
for(k=0;k<n;k++)
{
scanf("%s %c %s",arr1,&ch,arr2);
a=strlen(arr1);
b=strlen(arr2);
for(i=0;i<a;i++)
arr1[i]=toupper(arr1[i]);
for(i=0;i<b;i++)
arr2[i]=toupper(arr2[i]);

p=0;
c=a-1;
for(j=0;j<a;j++)
{
if(arr1[j]<=57)
p+=(long(arr1[j])-48)*(long(pow(16,c)));
else
p+=(long(arr1[j])-55)*(long(pow(16,c)));
c--;
}
q=0;
c=b-1;
for(j=0;j<b;j++)
{
if(arr2[j]<=57)
q+=(long(arr2[j])-48)*(long(pow(16,c)));
else
q+=(long(arr2[j])-55)*(long(pow(16,c)));
c--;
}
for(i=0;i<=MAX;i++)
{
num1[i]=0;
num2[i]=0;
}
c=MAX;
d=p;
while(d!=0)
{
num1[c]=d%2;
d=long(d/2);
c--;
}
c=MAX;
d=q;
while(d!=0)
{
num2[c]=d%2;
d=long(d/2);
c--;
}
for(i=1;i<=MAX;i++)
printf("%ld",num1[i]);
printf(" %c ",ch);

for(i=1;i<=MAX;i++)
printf("%ld",num2[i]);
printf(" = ");
if(ch=='+')
printf("%ld",p+q);
else
printf("%ld",p-q);
printf("\n");
}
}

[/c][/code]

User avatar
Riyad
Experienced poster
Posts: 131
Joined: Thu Aug 14, 2003 10:23 pm
Location: BUET
Contact:

try to think simply

Post by Riyad » Mon Nov 10, 2003 4:59 am

to solve the problem what u have to do is to write 2 simple routines .

1) which converts a hexa decimal string to integer
2) another which converts a decimal integer to a binary string .

do the following : -

get the number in hexa decimal . than convert both number to decimal . and do the operartion according to the operator . add or multiply ...

than convert the numbers and the result to its binary equivalent .

some people also opt for converting hexa decimal to binary rather than converting decimal to binary.go for which one u find easy.
Best Regards
Riyad
HOLD ME NOW ,, I AM 6 FEET FROM THE EDGE AND I AM THINKIN.. MAY BE SIX FEET IS SO FAR DOWN

User avatar
Riyad
Experienced poster
Posts: 131
Joined: Thu Aug 14, 2003 10:23 pm
Location: BUET
Contact:

Post by Riyad » Mon Nov 10, 2003 5:01 am

sorry no need to convert the result in to its binary equivalent . sorry for the mistake . :roll:
HOLD ME NOW ,, I AM 6 FEET FROM THE EDGE AND I AM THINKIN.. MAY BE SIX FEET IS SO FAR DOWN

Maarten
Experienced poster
Posts: 108
Joined: Sat Sep 27, 2003 5:24 pm

Post by Maarten » Mon Nov 10, 2003 11:15 am

I get wrong answer on this problem too.. and I really don't understand why. I passed all the test cases above.. anything I forgot? Anyone please help me !!
Here is my code:

[cpp]
#include <stdio.h>

void binary( char *result, int n )
{
int i;
for( i=12; i>=0; i-- ) {
result = '0' + (n & 1);
n >>= 1;
}
result[13] = 0;
}

int main(int argc, char *argv[])
{
int n, a, b, c, r;
char s[14], t[14];

scanf( "%d\n", &n );
while( n-- > 0 ) {
scanf( "%X %c %X", &a, &c, &b );
r = (c=='+') ? a+b : a-b;
binary(s, a);
binary(t, b);
printf( "%s %c %s = %d\n", s, c, t, r );
}

return 0;
}
[/cpp]

User avatar
shamim
A great helper
Posts: 498
Joined: Mon Dec 30, 2002 10:10 am
Location: Bozeman, Montana, USA

Post by shamim » Mon Nov 10, 2003 2:11 pm

I ran your code on my machine and it fails on the second sample input. But I don't understand why it should.

why did you define your main like this,

[cpp]int main(int argc, char *argv[]) [/cpp]

it is unnecessary.

Maarten
Experienced poster
Posts: 108
Joined: Sat Sep 27, 2003 5:24 pm

Post by Maarten » Mon Nov 10, 2003 3:51 pm

What operating system / compiler do you use ? Mine doesn't fail at those cases.
Btw I use Editplus and it creates a template like the one above. That's why I define my main like that. It never gives me problem before.

problem
New poster
Posts: 27
Joined: Mon Nov 10, 2003 12:40 am

446 wa plz help me.

Post by problem » Tue Nov 25, 2003 11:51 am

why wa plz help me.here is my code.
[c]
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<ctype.h>

binary(int sum)
{
int t[40];
int i=0,c;
memset(t,0,30);
while(sum>0)
{
t[i++]=fmod(sum,2)+48;
sum=sum/2;
}
c=13-i-1;
while(c>=0)
{
printf("0");
c--;
}
for(i=i-1;i>=0;i--)
printf("%c",t);
}

void main()
{
int i,j,k,sum=0,sum1=0,o,count,n,w,w1;
char t[150],l[150],b[2];
scanf("%d",&w);
for(w1=0;w1<w;w1++)
{
scanf("%s",t);
scanf("%s",b);
scanf("%s",l);
{
sum=0;
sum1=0;
i=strlen(t);
j=strlen(l);
for(k=0;k<i;k++)
{
if(isdigit(t[k]))
sum=sum+pow(16,k)*(t[k]-48);
if(isupper(t[k]))
sum+=pow(16,k)*(t[k]-55);
if(islower(t[k]))
sum+=pow(16,k)*(t[k]-87);
}
for(k=0;k<j;k++)
{
if(isdigit(l[k]))
sum1+=pow(16,k)*(l[k]-48);
if(isupper(l[k]))
sum1+=pow(16,k)*(l[k]-55);
if(islower(l[k]))
sum+=pow(16,k)*(l[k]-87);
}

binary(sum);
printf(" %c ",b[0]);
binary(sum1);
if(b[0]=='+')
count=sum+sum1;
else
count=sum-sum1;
printf(" = %d\n",count);
}
}
}
[\c]

Post Reply

Return to “Volume 4 (400-499)”