575 - Skew Binary

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

limon limon
New poster
Posts: 2
Joined: Tue Jul 09, 2002 10:36 am

575 - Skew Binary

Post 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]
rakeb
New poster
Posts: 42
Joined: Fri Aug 30, 2002 2:51 pm
Location: France

I think this is much better idea 575

Post 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);


}



}
luckyman
New poster
Posts: 3
Joined: Tue Oct 19, 2004 4:11 am

575 WA (JAVA)

Post 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
abhi
Learning poster
Posts: 94
Joined: Fri Nov 25, 2005 7:29 pm

575

Post 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;
}
ayon
Experienced poster
Posts: 161
Joined: Tue Oct 25, 2005 8:38 pm
Location: buet, dhaka, bangladesh

Post by ayon »

strrev() not allowed in unix
ishtiak zaman
----------------
the world is nothing but a good program, and we are all some instances of the program
abhi
Learning poster
Posts: 94
Joined: Fri Nov 25, 2005 7:29 pm

Post 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.............
ayon
Experienced poster
Posts: 161
Joined: Tue Oct 25, 2005 8:38 pm
Location: buet, dhaka, bangladesh

Post 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...
ishtiak zaman
----------------
the world is nothing but a good program, and we are all some instances of the program
Timo
Learning poster
Posts: 70
Joined: Tue Oct 11, 2005 2:44 am
Location: Indonesia

Post 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;
}
"Life is more beautiful with algorithm"
abhi
Learning poster
Posts: 94
Joined: Fri Nov 25, 2005 7:29 pm

Post by abhi »

oh i am really sorry..........
that was really a very foolish mistake on my part.
:oops:
hridoy
New poster
Posts: 21
Joined: Tue May 08, 2007 10:30 am
Location: Dhaka
Contact:

575

Post 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);
}
}
mmonish
Experienced poster
Posts: 109
Joined: Sun Mar 11, 2007 2:55 pm
Location: SUST

Post 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.
hridoy
New poster
Posts: 21
Joined: Tue May 08, 2007 10:30 am
Location: Dhaka
Contact:

Post by hridoy »

still I m getting CE :roll:
mmonish
Experienced poster
Posts: 109
Joined: Sun Mar 11, 2007 2:55 pm
Location: SUST

Post 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.
kbr_iut
Experienced poster
Posts: 103
Joined: Tue Mar 25, 2008 11:00 pm
Location: IUT-OIC, DHAKA, BANGLADESH
Contact:

575 still getting wa pliz anyone help

Post by kbr_iut »

now AC code deleted.
It is tough to become a good programmer.
It is more tough to become a good person.
I am trying both...............................
dewsworld
New poster
Posts: 12
Joined: Fri Aug 13, 2010 11:52 am

Skew Binary: got WA

Post 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 ;
}
Post Reply

Return to “Volume 5 (500-599)”