Page 1 of 2

575 - Skew Binary

Posted: Wed Jul 10, 2002 9:52 am
by limon limon
Anyone please say why? :o

[c]
#include<stdio.h>
#include<iostream.h>
#include<string.h>


void main(){

long power[32],test=1,count=0,val;
char num[35],n;
int len ,f;


power[1]=1;
power[2]=3;
power[3]=7;
power[4]=15;
power[5]=31;
power[6]=63;
power[7]=127;
power[8]=255;
power[9]=511;
power[10]=1023;
power[11]=2047;
power[12]=4095;
power[13]=8191;
power[14]=16383;
power[15]=32767;
power[16]=65535;
power[17]=131071;
power[18]=262143;
power[19]=524287;
power[20]=1048575;
power[21]=2097151;
power[22]=4194303;
power[23]=8388607;
power[24]=16777215;
power[25]=33554431;
power[26]=67108863;
power[27]=134217727;
power[28]=268435455;
power[29]=536870911;
power[30]=1073741823;
power[31]=2147483647;


while(scanf("%[\n]",&num)== 1)
test=1;

while(test)
{
scanf("%[^\n]",&num);

while(scanf("%[\n]",&n)==1 )
test=1;


if(!strcmp(num,"0\0"))
test=0 ;
else

{
val=0;
if(count>0)
printf("\n");

len=strlen(num);



for(f=0;f<len;f++)
{
if(num[f]>48)

val=val+(num[f]-48) * (power[len-f]);

}

printf("%ld",val);

count++ ;
}



}



}






[/c]

I think this is much better idea 575

Posted: Fri Aug 30, 2002 2:55 pm
by rakeb
I think this is much better idea

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

void main(){

double j;
char str[1000];
long l,i,sb=0;


while(gets(str))
{
l=strlen(str);
if(l==1)
if(str[0]-'0' == 0 )
break;
sb=0;
j = double(l);
for(i=0;i<l;i++)
{
sb+=(str-'0')*(pow(2,j)-1);
j--;
}
printf("%ld\n",sb);


}



}

575 WA (JAVA)

Posted: Sat Oct 23, 2004 8:56 pm
by luckyman
here is my code
[java]import java.io.IOException;

class Main
{
static String ReadLn (int maxLg)
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) && (lg == 0))
return (null);
return (new String (lin, 0, lg));
}
public static void main(String[] args) {
Main myWork = new Main();
myWork.start();
}
private void start()
{
String input;
long a;
while ((input = Main.ReadLn (255)) != null)
{
a = 0;
if (((int)input.charAt(0)-48) == 0)
break;
for (int i=0;i<input.length()-1;i++){
a = a + (((int)input.charAt(i)-48)*(power(2,input.length()-i-1)-1));
}
System.out.println(a);
}
}
long power(int n, int p)
{
long x = n;
for (int i=1;i<p;i++)
x = x*n;
return x;
}
}[/java]
I have tried input many different data
and they all work fine, but OJ gave me a WA
Can someone plz help, thanks

575

Posted: Fri Dec 02, 2005 3:56 pm
by abhi
this is my code for problem 575. i have compiled it on the turbo c compiler.
and it gives correct answer.
but OJ gives me compiler error.
i dont kno wat mistake i have made . plz help.

Code: Select all

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


int main()
{
	char a[100];
	long i=0,ans;
	while(gets(a))
{

	if(a[0]=='0')
  {
	exit(0);
  }
                 

    else
	{

		ans=0;
		strrev(a);
		for(i=0;a[i]!='\0';i++)
		{
			ans = ans+(a[i]-48)*(pow(2,i+1)-1);
			
		}
	printf("%ld\n",ans);

	}
return 0;
}

Posted: Fri Dec 02, 2005 8:19 pm
by ayon
strrev() not allowed in unix

Posted: Sat Dec 03, 2005 6:17 am
by abhi
i have removed the strrev function.

i replaced

Code: Select all

strrev(a)
by

Code: Select all

len=strlen(a);
for(i=0;i<len;i++)
{
    a[len-i]=a[i];
}
a[i]='\0';


also i have removed

Code: Select all

exit(0);
and replaced it by

Code: Select all

return 0;
since i had not included <process.h> in the code
but still CE.............

Posted: Sat Dec 03, 2005 7:49 am
by ayon
it's very much clear that. your program is missing a '}'. but for that reason, any compiler of this world would give the error message "compound statement missing }", i think. And believe me, your strrev() function wont reverse the string, swapping necessary, check it...

Posted: Sat Dec 03, 2005 7:59 am
by Timo
abhi wrote:i have removed the strrev function.

i replaced

Code: Select all

strrev(a)
by

Code: Select all

len=strlen(a);
for(i=0;i<len;i++)
{
    a[len-i]=a[i];
}
a[i]='\0';


also i have removed

Code: Select all

exit(0);
and replaced it by

Code: Select all

return 0;
since i had not included <process.h> in the code
but still CE.............

your strrev is wrong
it shoule be

Code: Select all

len=strlen(a);
for(i=0,len--;i<=len;i++,len--)
{
    c=a[i];
    a[i]=a[len];
    a[len]=c;
}

Posted: Sat Dec 03, 2005 9:20 am
by abhi
oh i am really sorry..........
that was really a very foolish mistake on my part.
:oops:

575

Posted: Fri May 25, 2007 9:04 pm
by hridoy
why the following program is CE,

#include<stdio.h>
main()
{
long long int n,z;
while(1)
{
scanf("%lld",&n);
if(n==0)
break;
long long int x,y,i,q=1;
z=0;
for(i=1;;i++)
{
x=n%10;
q*=2;
y=x*(q-1);
z+=y;
n/=10;
if(n==0)
break;
}
printf("%lld\n",z);
}
}

Posted: Sat May 26, 2007 3:36 am
by mmonish
U must use a return type for main function. U can use like this

void main()
{
//code
}
or
int main()
{
//code
return 0;
}

Hope it helps.

Posted: Sat May 26, 2007 11:15 am
by hridoy
still I m getting CE :roll:

Posted: Sat May 26, 2007 2:42 pm
by mmonish
I submit ur code by adding the following extra line

Code: Select all

printf("%lld\n",z); 
}
return 0;
it gives me WA. So u should think about ur solution.

Hope it helps.

575 still getting wa pliz anyone help

Posted: Wed Apr 02, 2008 8:24 pm
by kbr_iut
now AC code deleted.

Skew Binary: got WA

Posted: Sat Aug 28, 2010 8:30 am
by dewsworld
Can anyone please tell me why I get WA in this code... Thanks in advance

Code: Select all

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

#define uint32  unsigned int

int    main()
{
// freoen("in.txt", "rb", stdin );

    char    input[100] ;
    uint32  len ;
    int     i ;
    uint32  dec ;
    uint32  factor ;

    while( gets(input) != NULL )
    {
        if( (len = strlen( input )) == 1 )
            if( input[0] == '0' )
                break ;

        dec = 0 ;
        factor = 1 ;
        for( i = len-1 ; i >= 0 ; i-- )
        {
            dec = dec + (input[i]-'0')*(factor-1) ;

            factor = factor * 2 ;

        }

        printf("%u\n", dec ) ;

    }

    return 0 ;
}