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 » Tue Jun 17, 2003 9:40 am

Thanks a lot ( I got accepted :) ).

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

Post by szymcio2001 » Wed Jun 25, 2003 2:04 pm

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 » Thu Jul 03, 2003 3:00 pm

Thx, I already have AC

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

Post by Ronald29 » Thu Jul 03, 2003 3:00 pm

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 » Sat Dec 27, 2003 8:36 pm

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 » Sat Dec 27, 2003 8:37 pm

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 » Fri Aug 27, 2004 3:05 pm

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 » Fri Sep 03, 2004 4:01 pm

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 » Mon Sep 06, 2004 6:00 pm

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 );
        }
}

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

Post by Ghust_omega » Fri Sep 10, 2004 9:22 pm

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 !

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

AC.

Post by linux » Wed Jul 05, 2006 11:38 am

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 » Thu Dec 14, 2006 10:09 am

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 » Tue Jun 26, 2007 1:00 am

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 » Thu Jun 28, 2007 2:15 pm

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 » Thu Jun 28, 2007 5:35 pm

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

Post Reply

Return to “Volume 5 (500-599)”