594 - One Little, Two Little, Three Little Endians
Moderator: Board moderators
-
- New poster
- Posts: 2
- Joined: Tue Feb 10, 2009 8:34 pm
Please help with WA
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;
}
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;
}
-
- New poster
- Posts: 10
- Joined: Tue Jul 07, 2009 11:59 am
- Location: Ithaca, NY
Re: 594 : about negative number
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";
Re: 594 : about negative number
I don't understand why I keep getting WA in this . . . please someday help me out!
![:(](./images/smilies/icon_frown.gif)
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
First Submition woes.
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...
P.S. I just pasted my main method It is in a Main.java file with the correct Class definition as it was accepted.
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) {
}
}
problem with long/int in the problem 594
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:
AC code:
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.
-
- 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
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.
Re: 594 : about negative number
what's wrong with my code?
maybe there's something different between my compiler and the oj...i use dev-cpp. who can help me?? thanx..
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;
}
Re: First Submition woes.
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
Source: http://acm.uva.es/board/viewtopic.php?f=16&t=45714
Re: problem with long/int in the problem 594
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
594: Runtime Error
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:
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?
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));
}
}
}
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?
-
- Guru
- Posts: 5947
- Joined: Thu Sep 01, 2011 9:09 am
- Location: San Jose, CA, USA
Re: 594: Runtime Error
Use class Main
Check input and AC output for thousands of problems on uDebug!
Re: 594 - One Little, Two Little, Three Little Endians
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
![:(](./images/smilies/icon_frown.gif)
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;
}
Re: 594 - One Little, Two Little, Three Little Endians
Input
Acc Output
Code: Select all
0
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
Re: 594 - One Little, Two Little, Three Little Endians
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;
}
-
- 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
Try running your code on the sample input.
Check input and AC output for thousands of problems on uDebug!