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

aadarsh1093
New poster
Posts: 2
Joined: Tue Feb 10, 2009 8:34 pm

Please help with WA

Post by aadarsh1093 »

this is my code.. its running perfectly but i am not being able to understand why i am getting a wrong answer. plz help.
What i have done is first o have reversed each of the 8 bits of the number and then i have reversed the whole number.

#include<iostream.h>
int main()
{
long int a,g1,g2,g3,g4,x,g;
while(cin>>a)
{
long int c=1,m=8,num=0,num2=0,m1=32;
for(int i=0;i<m;i++)
{
g1=(c&(a>>i))<<(m-i-1);
num=num|g1;
g2=(c&(a>>(i+m)))<<(2*m-i-1);
num=num|g2;
g3=(c&(a>>(i+2*m)))<<(3*m-i-1);
num=num|g3;
g4=(c&(a>>(i+3*m)))<<(4*m-i-1);
num=num|g4;
}
for(int i=0;i<m1;i++)
{
g=(c&(num>>i))<<(m1-i-1);
num2=num2|g;
}
cout<<a<<" converts to "<<num2<<"\n";
}
return 0;
}
gaurav2289
New poster
Posts: 10
Joined: Tue Jul 07, 2009 11:59 am
Location: Ithaca, NY

Re: 594 : about negative number

Post by gaurav2289 »

Your approach is correct. Just do all calculations in unsigned int instead of long int. Modifications in your code will be--

Code: Select all

int a1;
unsigned int a,g1,g2,g3,g4,x,g;
while(cin>>a1)
{
a=(unsigned int)a1;
unsigned int c=1,m=8,num=0,num2=0,m1=32;
..rest all same 
cout<<a1<<" converts to "<<(int)num2<<"\n";
sauro
New poster
Posts: 4
Joined: Mon Aug 30, 2010 11:26 pm
Location: BUET, bangladesh
Contact:

Re: 594 : about negative number

Post by sauro »

I don't understand why I keep getting WA in this . . . please someday help me out! :(

Code: Select all

[c]
#include <stdio.h>

union
{
   long data;
   char bit[4];
}x,y;

int main()
{
	int i;
	while(scanf("%ld",&x.data)==1)
	{

	   for(i=0;i<4;i++)
	   {
	      y.bit[3-i]=x.bit[i];
	   }

	   printf("%ld converts to %ld\n",x.data,y.data);
	}
	return 0;
}
[/c]
Last edited by sauro on Wed Nov 09, 2011 9:24 pm, edited 1 time in total.
all that we see or seem is but a dream within a dream
raisdead
New poster
Posts: 6
Joined: Thu Dec 30, 2010 7:25 pm

First Submition woes.

Post by raisdead »

I solved problem 594 (One little two little three little Endians) a while a go and though I should check my answer so I submitted it and after playing around with the class name (It needed to be Main (Thanks! for the info!)) I submitted the code and I got Run time Error. When I put a try { .. } catch (Excpetion e) { ... } around all of my code in the main method it was accepted. Can anyone explain why this is the case? I would paste all of my code but I would not want to give the solution away...

Code: Select all

import java.util.Scanner;

    public static void main(String[] args) {
      try {
      Scanner in = new Scanner(System.in);
			
			String line = in.nextLine();
      while(!line.equals("")) {
				int thisEndian = Integer.parseInt(line);
        int thatEndian = flipEndian(thisEndian);
        System.out.println(thisEndian +" converts to "+thatEndian);
				line = in.nextLine();
      }
      } catch (Exception e) {
			
      }
    }
P.S. I just pasted my main method It is in a Main.java file with the correct Class definition as it was accepted.
songyy
New poster
Posts: 2
Joined: Mon Jan 17, 2011 5:44 pm

problem with long/int in the problem 594

Post by songyy »

URL: http://uva.onlinejudge.org/external/5/594.html

I really don't know why...

When I try to submit with datatype long, I got WA, while submit with int, I got AC... Here's the code:

WA code:

Code: Select all

#include <iostream>

using namespace std;

long converts(long a){
	long res=0;
	for(int i=0;i<4;i++){
        res<<=8; 
        res += a&255;
        a>>=8;
    }
	return res;
}

int main(){
	long in;
	
	while(scanf("%ld",&in)==1){
		cout << in << " converts to " << converts(in) << endl;
	}

	return 0;
}

AC code:

Code: Select all

#include <iostream>

using namespace std;

long converts(long a){
	int res=0;
	for(int i=0;i<4;i++){
        res<<=8; 
        res += a&255;
        a>>=8;
    }
	return res;
}

int main(){
	long in;
	
	while(scanf("%ld",&in)==1){
		cout << in << " converts to " << converts(in) << endl;
	}

	return 0;
}
What doesn't kills you makes you stronger.
zobayer
Experienced poster
Posts: 110
Joined: Tue May 06, 2008 2:18 pm
Location: CSE-DU, Bangladesh
Contact:

Re: problem with long/int in the problem 594

Post by zobayer »

that's weird! I have also faced this on some other problems. Can anyone please clarify if there's anything strange with type long? I think cout causes the problem.
You should not always say what you know, but you should always know what you say.
jcvb
New poster
Posts: 4
Joined: Tue Feb 15, 2011 5:35 pm

Re: 594 : about negative number

Post by jcvb »

what's wrong with my code?

Code: Select all

#include <stdio.h>
int main()
{
    long n;
    while (scanf("%ld",&n)!=EOF){
          unsigned long tmp,ans=0;
          printf ("%ld converts to ",n);
          tmp = n;
          ans|=(tmp<<24);
          ans|=((tmp<<8)&0x00ff0000);
          ans|=((tmp>>8)&0x0000ff00);
          ans|=(tmp>>24);
          printf ("%ld\n",ans);
    }          
    return 0;
}
maybe there's something different between my compiler and the oj...i use dev-cpp. who can help me?? thanx..
raisdead
New poster
Posts: 6
Joined: Thu Dec 30, 2010 7:25 pm

Re: First Submition woes.

Post by raisdead »

It appears that my scanner will through an exception when Scanner receives the EOF.

Source: http://acm.uva.es/board/viewtopic.php?f=16&t=45714
plamplam
Experienced poster
Posts: 150
Joined: Fri May 06, 2011 11:37 am

Re: problem with long/int in the problem 594

Post by plamplam »

The long datatype on UVA is of 8 bytes as far as I remember. idk why
You tried your best and you failed miserably. The lesson is 'never try'. -Homer Simpson
piyukr
New poster
Posts: 17
Joined: Sun Jan 26, 2014 10:35 am

594: Runtime Error

Post by piyukr »

The problem is about taking numerical inputs from the user and converting it from Little endian to Big Endian and vice versa:
Here is the code:

Code: Select all

import java.lang.*;
import java.io.*;
import java.util.Scanner;
class Little_endians {
    public static void main(String[] args) throws IOException {
            Scanner sc=new Scanner(System.in);
            int x;
            while(sc.hasNextInt()==true){
                    x=sc.nextInt();
                    System.out.println(x+" converts to "+Integer.reverseBytes(x));
             }
	    }
    }
The logic for the program is correct , it runs fine on all the sample inputs too .
For this solution, I got :Your submission with number 13033077 for the problem 594 - One Little, Two Little, Three Little Endians has failed with verdict Runtime error.
This means that the execution of your program didn't finish properly. Remember to always terminate your code with the exit code 0.
Can someone help understand the problem in my code?
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 594: Runtime Error

Post by brianfry713 »

Use class Main
Check input and AC output for thousands of problems on uDebug!
RookiE3
New poster
Posts: 16
Joined: Tue Feb 18, 2014 7:59 pm

Re: 594 - One Little, Two Little, Three Little Endians

Post by RookiE3 »

I get correct answer for all the test cases I found so far, but yet I get WA :(
Please tell me what am I doing wrong here

Code: Select all

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

unsigned Pow(int a, int p)
{
    if (p == 0)
        return 1;
    unsigned ret = a, i;
    for (i = 1; i < p; i++)
        ret *= a;
    return ret;
}

void twosComplement(char *n)
{
    int i;
    for (i = 0; i < 32; i++)
        n[i] = n[i] == '0' ? '1' : '0';
    for (i = 31; i >= 0; i--)
    {
        if (n[i] == '0')
        {
            n[i] = '1';
            break;
        }
        else
            n[i] = '0';
    }
}

char* toBinary(int dec)
{
    char temp[33], bin[33];
    int i = 0, j;
    dec = abs(dec);
    while (dec)
    {
        temp[i++] = dec % 2 + '0';
        dec /= 2;
    }
    for (j = 0; j<32 - i; j++)
        bin[j] = '0';
    while (i > 0)
        bin[j++] = temp[--i];
    bin[j] = 0;
    return bin;
}

int toDecimal(char *n)
{
    int i, ret = 0;
    for (i = 0; i < 32; i++)
        ret += (n[i] - '0') * Pow(2, 31 - i);
    return ret;
}

char* toAlternateEndian(char *n)
{
    char ret[33];
    int i;
    ret[0] = 0;
    for (i = 24; i >= 0; i -= 8)
        strncat(ret, n + i, 8);
    return ret;
}

int main()
{
    int dec;
    char bin[33], otherEndian[33];
    while (scanf("%d", &dec) == 1)
    {
        if (dec == -2147483648)
        {
            printf("-2147483648 converts to 128\n");
            continue;
        }
        strcpy(bin, toBinary(dec));

        if (dec < 0)
            twosComplement(bin);

        strcpy(otherEndian, toAlternateEndian(bin));

        if (otherEndian[0] == '1')
        {
            twosComplement(otherEndian);
            printf("%d converts to %d\n", dec, -toDecimal(otherEndian));
        }
        else
            printf("%d converts to %d\n", dec, toDecimal(otherEndian));
    }
    return 0;
}
lighted
Guru
Posts: 587
Joined: Wed Jun 11, 2014 9:56 pm
Location: Kyrgyzstan, Bishkek

Re: 594 - One Little, Two Little, Three Little Endians

Post by lighted »

Input

Code: Select all

0
Acc Output

Code: Select all

0 converts to 0
A person who sees the good in things has good thoughts. And he who has good thoughts receives pleasure from life... Bediuzzaman
Rika71
New poster
Posts: 11
Joined: Sat Apr 26, 2014 9:42 pm

Re: 594 - One Little, Two Little, Three Little Endians

Post by Rika71 »

getting WA

Code: Select all

#include <stdio.h>
union tag
{
    long n;
    char a[4];
}k;
int main()
{
    long i,x,y;
    char t;
    while(scanf("%ld",&i)==1)
    {
        k.n=i;
        /*for(x=0;x<4;x++) printf("%d ",k.a[x]);*/
        for(x=0,y=3;x<=y;x++,y--)
        {
            t=k.a[x];
            k.a[x]=k.a[y];
            k.a[y]=t;
        }
        printf("%ld converts to %ld\n",i,k.n);
    }
    return 0;
}
brianfry713
Guru
Posts: 5947
Joined: Thu Sep 01, 2011 9:09 am
Location: San Jose, CA, USA

Re: 594 - One Little, Two Little, Three Little Endians

Post by brianfry713 »

Try running your code on the sample input.
Check input and AC output for thousands of problems on uDebug!
Post Reply

Return to “Volume 5 (500-599)”