Você está na página 1de 7

APRIL CODING CONTEST 2010

1. To find whether an integer (<2^31) entered is a power of 2 or


not. The condition was that code should be as small as possible
and should not use any semicolons. The input begins with integer
T followed by T cases?

Example: Table of input & output

Input: Output:
2
12 NO
128 YES

Method 1: /* Actual solution for given Qn */

/*for input T cases*/

main(t,n)
{
if(scanf("%d",&t))
while(scanf("%d",&n),puts(n&n-1?"no":"yes"),--t)
{}
}

Solution 2: /*for infinite cases, without input T*/

main(i)
{
while(~scanf("%d",&i)&&puts(i&i-1?"no":"yes"))
{}
}

CSEHACKERZ 2010
APRIL CODING CONTEST 2010

2. To find whether the binary representation of a number is


palindrome or not. The input begins with integer T followed by T
cases?

(Example: Binary number of 15 is 1111 is palindrome.


But Binary number of 12 is 1100 is not a palindrome)
Example :Table of input & output

Input: Output:
2
12 NO
15 YES

Method 1: /* Actual solution for given Qn */

/* for input T cases*/

main(v,r,t,x)
{
scanf("%d",&t);
while(t--)
{
for(scanf("%d",&v),x=v,r=0;v;r<<=1,r|=v&1,v>>=1)
{}
puts(r-x?"NO":"YES");
}
}

Method 2:
/*for infinite cases, without input T, Using Recursion.*/

int a,t;

main(c,b)
{
c?main(c/2,b*2+c%2):t++/2&&puts(a-b?"NO":"YES"),
~scanf("%d",&a)&&main(a,0);
}

CSEHACKERZ 2010
APRIL CODING CONTEST 2010

3.Program without main() ?

Compile and run this. It works fine but how? Actually it’s a very good
example of the preprocessor directive #define and token pasting or
token merging operator ‘##’

Method:

#include"stdio.h"
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)

int begin()
{
printf(" hello ");
}

/* Note run at command prompt after compiling enter t.exe if


file name is t.c */

4.SWAP WITHOUT USING (semicolon,pointer,temporary variable,arithmetic


operators).

Solution in C:

/* C implementation-swapping */

main(x,y)
{
if(printf("\nEnter a and b:"))
if(scanf("%d %d",&x,&y),printf("\n Before swap: a=%d b=%d",x,y))
if(x=x^y,y=x^y,x=x^y,printf("\n After swap: a=%d b=%d",x,y)){}
}

CSEHACKERZ 2010
APRIL CODING CONTEST 2010

Solution in C++:

/* C++ implementation-swapping */

# include<conio.h>
# include<iostream.h>
void main(int x,int y)
{
if(clrscr(),cout<<"\nEnter a and b:",cin>>x>>y)
if(cout<<"\n Before swap: a="<<x<<" b= "<<y)
if(x=x^y,y=x^y,x=x^y,cout<<"\n After swap: a="<<x<<"
b="<<y,getch())
{}
}

5. COMPUTE Factorial & Fibbonacci Without Using semicolon?

Solution in C:

main(a,b,c,n,f)
{

/*Fibbonaci procedure here */

if(printf("\nEnter n for fibbonaci:"),scanf("%d",&n),printf("\n


Fibbonaci series:"))
if(a=-1,b=1,c=0,n!=0)
while(c=a+b,a=b,b=c,n--)
if(printf("\n%d",c)){}

/*Factorial procedure here */

if(printf("\nEnter n for factorial:"),scanf("%d",&n),f=1,n!=0)


while(f*=n,--n){}
if(printf("\n Factorial:%d",f),0)
{}
}

CSEHACKERZ 2010
APRIL CODING CONTEST 2010

Solution in C++:

/*Fibbonaci */

# include<conio.h>
# include<iostream.h>

void main(int a,int b,int c,int n)


{
if(clrscr(),cout<<"\nEnter n:",cin>>n,cout<<"\n Fibbonaci
series:",a=-1,b=1,c=0,n!=0)
while(c=a+b,a=b,b=c,n--)if(cout<<"\n"<<c){}
if(getch(),0)
{}
}

/* Factorial */

# include<conio.h>
# include<iostream.h>

void main(int n,int f)


{
if(clrscr(),cout<<"\nEnter n:",cin>>n,f=1,n!=0)
while(f*=n,--n){}
if(cout<<"\n Factorial:"<<f,getch(),0)
{}
}

CSEHACKERZ 2010
APRIL CODING CONTEST 2010

6. Your task is to add two reversed numbers and output their reversed
sum.

Details:
Of course, the result is not unique because any particular number is a
reversed form of several numbers (e.g. 21 could be 12, 120 or 1200
before reversing). Thus we must assume that no zeros were lost by
reversing (e.g. assume that the original number was 12).

Input:

The input consists of N cases (equal to about 10000). The first line
of the input contains only positive integer N. Then follow the cases.
Each case consists of exactly one line with two positive integers
separated by space. These are the reversed numbers you are to add.

Output:

For each case, print exactly one line containing only one integer -
the reversed sum of two reversed numbers. Omit any leading zeros in
the output.
Example: Table of input & output

Input: Output:
1
24 1 34

/*

Input a Case (say T=1)

Reversing Input pairs of integers (say 24 into 42 and 1 into 1 )

Adding both values 42+1=43

Reversing the result of sum 34

*/

CSEHACKERZ 2010
APRIL CODING CONTEST 2010

Solution:

int c[10000],i=0;

/* Function for reversing a number */


int revr(int x)
{
int rev=0,t;
while(x)
{
t=x%10;
rev+=t;
rev*=10;
x/=10;
}
return rev/10;
}

main(a,b,t)
{
int x,y,*sum,j=0;
scanf("%d",&t);

while((t--)>0)
{
scanf("%d %d",&a,&b);

/* storing reversed sum of reversed integers in a array */


c[i]=revr(revr(a)+revr(b));
i++;
}
/*printing reversed Sum from array */
while(j<i)
{
printf("\nSum=%d",c[j]);
j++;
}

CSEHACKERZ 2010

Você também pode gostar