594 - One Little, Two Little, Three Little Endians

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

Moderator: Board moderators

globi
New poster
Posts: 15
Joined: Wed Apr 23, 2003 2:44 pm
Location: Warsaw
Contact:

Post by globi »

Thanks a lot ( I got accepted :) ).

szymcio2001
New poster
Posts: 38
Joined: Mon Dec 09, 2002 1:53 pm
Location: Poznan, Poland

Post by szymcio2001 »

Some additional test cases for everyone who has a problem with negative input:
0 -> 0
-1 -> -1
-2 -> -16777217
-65536 -> 65535

Ronald29
New poster
Posts: 15
Joined: Sat May 24, 2003 3:57 am

Post by Ronald29 »

Thx, I already have AC

Ronald29
New poster
Posts: 15
Joined: Sat May 24, 2003 3:57 am

Post by Ronald29 »

Thx, I've already got AC

aakash_mandhar
New poster
Posts: 38
Joined: Thu Dec 11, 2003 3:40 pm
Location: Bangalore

Post by aakash_mandhar »

Try this code. this is very easy problem
[cpp]
Code removed.
Thx Shamim for the suggestion
[/cpp]

Hope u like the code
Last edited by aakash_mandhar on Wed Jan 28, 2004 4:43 pm, edited 1 time in total.
...I was born to code...

aakash_mandhar
New poster
Posts: 38
Joined: Thu Dec 11, 2003 3:40 pm
Location: Bangalore

Easy sol to endian...

Post by aakash_mandhar »

Removed.. Thx Shamim for the suggestion
Last edited by aakash_mandhar on Wed Jan 28, 2004 4:42 pm, edited 1 time in total.
...I was born to code...

RustB
New poster
Posts: 16
Joined: Mon Jun 14, 2004 5:08 pm

594- One Little, Two Little, Three Little Endians WA

Post by RustB »

This is giving me correct answers with my trial inputs, but the judge returns W.A. Why is that?
[c]
#include <stdio.h>

union inp
{
long num;
char a[4];
} in,out;

int main(void)
{
while(!feof(stdin))
{
int i;
in.num=out.num=0;
scanf("%d",&in.num);
for(i=0;i<4;i++)
out.a=in.a[3-i];
printf("%d converts to %d\n",in.num,out.num);
}
}
[/c]

Ryan Pai
Learning poster
Posts: 67
Joined: Fri Jul 04, 2003 9:59 am
Location: USA
Contact:

Post by Ryan Pai »

There's probably an end-of-line character at the end of the data. You should always make your solution not depend on there being/not being one.

So after it reads the last number, there's still that last character, the end-of-line. So the feof() returns false. But the next iteration you don't read a number, so it outputs 0.

Also, the endianness of your system might be different than the judges (unless you checked what they use and compared it to what you use).
I'm always willing to help, if you do the same.

acmforacm
New poster
Posts: 1
Joined: Mon Sep 06, 2004 5:56 pm

594 - got WA

Post by acmforacm »

i got WA~
who can help me ?

Code: Select all

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

main()
{
        long long n , num;
        int i , j , k;
        int binary[4][8];
        while ( scanf ( " %lld" , &n ) == 1 ){
                printf ( "%lld" , n );
                for ( i=0; i<4; ++i )
                        for ( j=0; j<8; ++j )
                                binary[i][j] = 0;
                i = j = num = 0;
                while ( n >= 1 ){
                        binary[i][j++] = n % 2;
                        n /= 2;
                        if ( j == 8 ){
                                j = 0;
                                i++;
                        }
                }
                for ( i=3 , k=0; i>=0; --i , ++k ){
                        for ( j=0; j<8; ++j ){
                                num += binary[i][j] * pow( 2 , ( ( k * 8 ) + j ) );
                        }
                }
                printf ( " converts to %lld\n" , num );
        }
}

Ghust_omega
Experienced poster
Posts: 115
Joined: Tue Apr 06, 2004 7:04 pm
Location: Venezuela

Post by Ghust_omega »

Hi acmforacm i found one bug in your code not handlle negative inputs fine see what is the ouput for this
-7 , -5 is 0 you have to check your code
Hope its Helps :)
Keep posting !

linux
Learning poster
Posts: 56
Joined: Sat Jul 01, 2006 12:21 pm
Location: CA-95054
Contact:

AC.

Post by linux »

Hey, RustB! Don't be worried about endians they are same as you did. But about end-of-line character ryan pai is right. I think you better use scanf by which I got AC from you program.
Solving for fun..

B.E
New poster
Posts: 7
Joined: Tue May 24, 2005 5:20 am

594 Problem

Post by B.E »

I would just like to say that the problem is very easy(did it in under a minute). The code I used was (which the CPU time was 0.004 seconds) The following. It works by using the 80386 assembly instruction to byte swap an integer (the instruction is often used to convert big endian numbers to little endian numbers and vice versa).

Code: Select all

#include <stdio.h>
#include <stdlib.h>

int main(){
	char buff[13];
	register long l;
	while (gets(buff)){
		l = atol(buff);
		__asm__ __volatile__ ("bswap %0" : "+r" (l));
		printf("%s converts to %d\n", buff, l);
	}
	return 0;
}

lovemagic
Learning poster
Posts: 52
Joined: Thu Oct 02, 2003 11:38 am

Post by lovemagic »

can somebody explain when the output should be negetive? & how can i handle negetive number?
khobaib

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan »

lovemagic wrote:1. can somebody explain when the output should be negetive?
2. how can i handle negetive number?
1. What is the value of binary(1111111...111 -> 32 1's) for a signed 32-bit integer?
2. No special condition I think.
Ami ekhono shopno dekhi...
HomePage

lovemagic
Learning poster
Posts: 52
Joined: Thu Oct 02, 2003 11:38 am

Post by lovemagic »

Thanx for ur reply.Now i understood.
Got AC...... :D
khobaib

Post Reply

Return to “Volume 5 (500-599)”