
594 - One Little, Two Little, Three Little Endians
Moderator: Board moderators
- szymcio2001
- New poster
- Posts: 38
- Joined: Mon Dec 09, 2002 1:53 pm
- Location: Poznan, Poland
-
- New poster
- Posts: 38
- Joined: Thu Dec 11, 2003 3:40 pm
- Location: Bangalore
Try this code. this is very easy problem
[cpp]
Code removed.
Thx Shamim for the suggestion
[/cpp]
Hope u like the code
[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...
-
- New poster
- Posts: 38
- Joined: Thu Dec 11, 2003 3:40 pm
- Location: Bangalore
Easy sol to endian...
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...
594- One Little, Two Little, Three Little Endians WA
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]
[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]
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).
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.
594 - got WA
i got WA~
who can help me ?
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
AC.
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..
594 Problem
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;
}
1. What is the value of binary(1111111...111 -> 32 1's) for a signed 32-bit integer?lovemagic wrote:1. can somebody explain when the output should be negetive?
2. how can i handle negetive number?
2. No special condition I think.
Ami ekhono shopno dekhi...
HomePage
HomePage