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

popel
New poster
Posts: 33
Joined: Fri Mar 15, 2002 2:00 am
Location: Dhaka, Bangladesh
Contact:

Post by popel »

It seems you are new. I think you should carefully examine the following code. Actually this problem is a very easy one.

[cpp]
#include<stdio.h>

void printbinary13(int x);

void printbinary13(int x){
int c,v=0x1000;
for(c=0;c<13;c++){
if(x&v)putchar('1');else putchar('0');
v>>=1;
}
}

void main(){
int a,b,n,i,r;
char op;
scanf("%d",&n);
for(i=0;i<n;i++){
scanf("%X %c %X",&a,&op,&b);
if(op=='+')r=a+b;else r=a-b;
printbinary13(a);
printf(" %c ",op);
printbinary13(b);
printf(" = %d\n",r);
}
}
[/cpp]
μδ. ταηνιπ αλ αμιη

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

Post by shamim »

The Basic idea is you can directly read hexadecimal number using
scanf();
scanf("%X %c %X",&a,&op,&b);
the %X refers to hexadecimal.

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

shamim

Post by osan »

wat is difference between %X %x at the time of scanf():
will u tell me please.

as i know it can fell effect at the time of printf();

popel
New poster
Posts: 33
Joined: Fri Mar 15, 2002 2:00 am
Location: Dhaka, Bangladesh
Contact:

Post by popel »

probably no problem when scanf. but why not you experiment...?
μδ. ταηνιπ αλ αμιη

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

thanx popel

Post by osan »

sorry i misunderstood shamim talk.
i know that wont fell effect. that y i asked that.

_.B._
Experienced poster
Posts: 160
Joined: Sat Feb 07, 2004 7:50 pm
Location: Venezuela
Contact:

446 > Kibbles `n' Bits `n' Bits `n' Bits > Help!.

Post by _.B._ »

Greetings!.
I thought this wasn't going to be a tough problem, but I'm having a hard time trying to solve it :roll:
Need help. I've tested it with Input from forum, and it seems to work, but I keep getting WAs from all modifications I've made to it.
[pascal]{ Bernardo E. L
_.

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

not valid

Post by sohel »

2
0 - FFF
FFF + FFF
For the above inputs:

I don't think the first one is valid and thus will not be in the judge's data..
... for the second one my AC program outputs:

0111111111111 + 0111111111111 = 8190

_.B._
Experienced poster
Posts: 160
Joined: Sat Feb 07, 2004 7:50 pm
Location: Venezuela
Contact:

Thanks. And still need help :)

Post by _.B._ »

Thanks for the quick response sohel!.
Wanted to be sure no negative responses were allowed.
Still need to know what's wrong with my program :roll:
What am I missing?? :o
_.

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

Post by sohel »

I am not a pascal coder but you can try the following input/output:

Input:
[c]
10
A + B
FFF - 123
36 + 2
0 + 0
3 - 0
23 - 1
A46 - 1A
F - 1
14 - 13
988 - FA
[/c]

output
[c]
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
[/c]

_.B._
Experienced poster
Posts: 160
Joined: Sat Feb 07, 2004 7:50 pm
Location: Venezuela
Contact:

Thanks!, but still...

Post by _.B._ »

Thanks again sohel!.
My program throws the exact same answer as posted by you and others in other posts :cry:
Either one of us is going insane, me or the OnlineJudge :o
Because I just tried to solve problem 499 and got the same stats for the WA as I got for the 3 WAs for the 446:
Wrong Answer - 0:00.002 - 64
_.

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

Post by sohel »

The best is to redo the code from scratch
It is very difficult to find mistakes on problems of this type.
I am sure you made a very minor mistake and redoing it will lead to success. :)

_.B._
Experienced poster
Posts: 160
Joined: Sat Feb 07, 2004 7:50 pm
Location: Venezuela
Contact:

Accepted!.

Post by _.B._ »

Thanks for the help sohel!.
Tried to make it in a different way, and found out the mistake was in reading the #13 and #26.
Now I'm changing to eoLn and eoF and getting ACs!.
Keep posting!.
_.

challenger
New poster
Posts: 6
Joined: Sun Jan 23, 2005 8:21 pm

446 - Returning a string...

Post by challenger »

So... I am having difficulty returning a string...
I used a pointer...
char *pt;
to point to my input string.. I debugged the program under VC6.0 and pt handles the right string.. the problem is that its content is not properly returned...

Code: Select all

#include <stdio.h>

char *bin(int num);

main()
{
	int i,n,hex1,hex2;
	char op;

	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		scanf("%X %c %X",&hex1,&op,&hex2);
		printf("%s %c %s = %d\n",bin(hex1),op,bin(hex2),op=='+' ? hex1+hex2 : hex1-hex2);
	}	
}

char *bin(int num)
{
	char temp[13]="0000000000000",*pt=temp;
	int i=12;

	while(num!=0)
	{
		temp[i]=48+num%2;
		num=num/2;
		i--;		
	}
	temp[13]=0;
	
	return pt;
}
The output I get is
C:\Valladolid\Debug>446 < 446.txt

Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba »

You're trying to use a pointer to an array that no longer exists.

[_TANG_]
New poster
Posts: 15
Joined: Wed May 04, 2005 12:28 am
Location: Mexico

446 - WA!!

Post by [_TANG_] »

Hello, I solved this problem and all I/O test I've seen in all posts are OK in my program, But I can't get AC .... Can any one give me some criticals I/O for this problem? ... :cry:

Here's my code, am I missing something?

Code: Select all


#include <iostream.h>
#include <math.h>
#include <string.h>
#include <ctype.h>

char *binario[16] = {"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001","1010","1011","1100","1101","1110","1111"};
	
void cambia_hex_bin(char*,char*);
int cambia_bin_dec(char*);
	
void main()
{
	char hex1[5], hex2[5], op[1];
	int num1, num2, c, n;
	
	cin >> n;
	
	for(c = 0; c < n; c++)
	{
		char temp[14] = {'\0'};
		
		cin>> hex1 >> op >> hex2;
		
		cambia_hex_bin(hex1,temp);
		num1 = cambia_bin_dec(temp);
		
		cout << temp << " " << op;
		
		memset(temp,'\0',14);
		
		cambia_hex_bin(hex2,temp);
		num2 = cambia_bin_dec(temp);
		
		cout << " " << temp << " = ";
		
		if(strcmp(op,"+") == 0)
			cout << num1 + num2 << endl;
		else
			cout << num1 - num2 << endl;
	}
}

int cambia_bin_dec(char *bin)
{
	int c, pos = 0;
	double suma = 0.0;
	
	for(c = strlen(bin)-1; c >= 0; c--, pos++)
		if(bin[pos] == '1')
			suma += pow(2,c);
		
	return (int)suma;
}

void cambia_hex_bin(char *hex, char *bin)
{
	int c = 0, len;
	
	strcat(bin,"0");
	
	len = strlen(hex);
	
	for(c = len; c < 3; c++)
		strcat(bin,"0000");
	
	c = 0;
	
	while(hex[c])
	{
		if(isalpha(hex[c]))
			strcat(bin,binario[hex[c] - 55]);
		else
			strcat(bin,binario[hex[c] - 48]);
		
		c++;
	}
}
___________________
[_TANG_]

Post Reply

Return to “Volume 4 (400-499)”