Page 3 of 4
Please help with WA
Posted: Sat Jul 11, 2009 1:07 pm
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;
}
Re: 594 : about negative number
Posted: Sun Jul 12, 2009 6:31 pm
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";
Re: 594 : about negative number
Posted: Tue Aug 31, 2010 12:16 am
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]
First Submition woes.
Posted: Thu Dec 30, 2010 7:31 pm
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.
problem with long/int in the problem 594
Posted: Mon Jan 17, 2011 6:01 pm
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;
}
Re: problem with long/int in the problem 594
Posted: Wed Feb 16, 2011 10:58 am
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.
Re: 594 : about negative number
Posted: Sat Jun 04, 2011 7:45 am
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..
Re: First Submition woes.
Posted: Wed Jul 20, 2011 4:21 am
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
Re: problem with long/int in the problem 594
Posted: Thu Aug 04, 2011 8:41 pm
by plamplam
The long datatype on UVA is of 8 bytes as far as I remember. idk why
594: Runtime Error
Posted: Sun Jan 26, 2014 11:00 am
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?
Re: 594: Runtime Error
Posted: Mon Jan 27, 2014 9:28 pm
by brianfry713
Use class Main
Re: 594 - One Little, Two Little, Three Little Endians
Posted: Fri Aug 29, 2014 3:59 pm
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;
}
Re: 594 - One Little, Two Little, Three Little Endians
Posted: Sun Aug 31, 2014 7:05 pm
by lighted
Re: 594 - One Little, Two Little, Three Little Endians
Posted: Mon Sep 15, 2014 8:47 pm
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;
}
Re: 594 - One Little, Two Little, Three Little Endians
Posted: Tue Sep 16, 2014 8:31 pm
by brianfry713
Try running your code on the sample input.