Wednesday 30 January 2013

10373. Help Balaji! Problem code: ABA12A

for j in range(input()):
    a,c=raw_input().split()
    print c

42. Adding Reversed Numbers Problem code: ADDREV

def r(n):
    s=0
    while n>=1:
        a=n%10
        s=s*10+a
        n/=10
    return s

for i in range(input()):
    x,y=raw_input().split()
    print r(r(int(x))+r(int(y)))     

9722. Insertion Sort Problem code: CODESPTB

#include<stdio.h>
int ans;
void merge(int a[] , int left , int mid , int right)
{
    int temp[200000];
    int i=left, j=mid+1 , k=0;
   
    while((i<=mid)&&(j<=right))
    {
        if(a[i]<=a[j])
            temp[k++]=a[i++];
   
        else
        {
            ans=ans+mid-i+1;
            temp[k++]=a[j++];
       }
    }
   
    while(i<=mid)
        temp[k++]=a[i++];
  
    while(j<=right)
        temp[k++]=a[j++];
  
    for(i=0;i<k;i++)
        a[left+i]=temp[i];
}

void merge_sort(int a[],int left,int right)
{
    int mid;
   
    if(left>=right)
        return;
   
    else
    {
        mid=(left+right)/2;
        merge_sort(a,left,mid);
        merge_sort(a,mid+1,right);
        merge(a,left,mid,right);
    }
}


int main()
{
    int a[200000];
    int t,n,i;
    scanf("%d",&t);
    while(t--)
    {
        ans=0;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
       
        merge_sort(a,0,n-1);
        printf("%d\n",ans);
    }
    return 0;
}

11. Factorial Problem code: FCTRL

#include <stdio.h>
int main()
{
    long long int ans,num,test;
    scanf("%d",&test);

    while(test--)
    {
        scanf("%lld",&num);
        ans=0;
        while(num)
        {
            num/=5;
            ans += num;
        }
        printf("%lld\n",ans);
    }
    return 0;
}

11932. Amz Rock Problem code: AMZRCK

p=5**.5;f=lambda n:((1+p)**n-(1-p)**n)/(2**n*p)
i=input
for j in range(i()):
 print int(f(i()+2))

5917. Factorial length Problem code: LENGFACT

#include <cmath>
#include <cstdio>
using namespace std;
int main()
{
    double num, length, pi = acos(-1.0);
    int t;
    scanf("%d", &t);
    while(t--)
        scanf("%lf", &num),printf("%.0lf\n", num<3.0? 1.0 : floor((num*log(num)-num+(log(2.0*pi*num))/2.0)/log(10.0))+1.0 );
    return 0;
}

2148. Candy III Problem code: CANDY3

#include<stdio.h>
int main()
{
    long long int t , n , i ,sum ;
    scanf("%lld", &t);
    while(t--)
    {
        sum=0;
        printf("\n");
        scanf("%lld",&n);
        long long int a[n];
        for (i=0;i<n;i++)
        {
            scanf("%lld",&a[i]);
            sum =sum+a[i];
            sum=sum%n;
        }

            if(sum==0)
                printf("YES\n");
           
            else
                printf("NO\n");   
    }
    return 0;
}

23. Pyramids Problem code: PIR

#include<stdio.h>
#include<math.h>
int main()
{
    int t;
    double  a,b,c,d,e,f,A,B,C,D,E,F;
    double vol;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lf%lf%lf%lf%lf%lf",&a,&c,&e,&b,&d,&f);
        A=a*a;
        B=b*b;
        C=c*c;
        D=d*d;
        E=e*e;
        F=f*f;
        vol = sqrt((-A*B*C - A*D*E - B*D*F - C*E*F + A*C*D + B*C*D + A*B*E + B*C*E + B*D*E +C*D*E + A*B*F + A*C*F +A*D*F + C*D*F + A*E*F + B*E*F - C*C*D - C*D*D - B*B*E - B*E*E- A*A*F - A*F*F)/144.0);
        printf("%.4lf\n",vol);   
    }
    return 0;
}

2523. Mispelling Problem code: GNY07A

#include<stdio.h>
#include<math.h>
int main()
{
    char c;
    int d,i,j,ct,n;
    scanf("%d",&n);
    ct =1;
    for(j=1;j<=ceil(2*n);j++)
    {
        scanf("%d",&d);
        i=1;
        while((c=getchar())!='\n'&& c!=' ' )
        {
            if (i==1)
                printf("%d ",j/2);
             if (c!=' ' && i!=d)
                printf("%c",c);           
            i++;
        }
            printf("\n");
    }
    return 0;
}

7406. Beehive Numbers Problem code: BEENUMS

while 1:
    n=input()
    if n==-1:
        break
    k=float((-3+pow(-3+12*n,1.0/2))/6)
    d=int(k)
    if d-k==0:
        print "Y"
    else:
        print "N"   

7733. Happy Numbers I Problem code: HPYNOS

Squares = dict([(m, int(m)**2) for m in "0123456789"])
def is_happy(n):
  s = []
  count=0
  while n not in s:
      if n==1:
          return count
        s.append(n)
        n = sum(Squares[digits] for digits in str(n))
        count +=1
  return -1
print is_happy(input())

450. Enormous Input Test Problem code: INTEST

#include<stdio.h>
int main()
{
    long long int n,k,x,c=0;
    scanf("%lld%lld",&n,&k);
   
    while(n--)
    {
        scanf("%lld",&x);
        if(x%k==0)
            c++;
    }
    printf("%lld\n",c);
    return 0;
}

Monday 28 January 2013

1030. Triple Fat Ladies Problem code: EIGHTS

 #include<stdio.h>
int main()
{
    int test;
      long long int k,temp;
      scanf("%d",&test);
   
      while(test--)
      {
          scanf("%lld",&k);
          temp=(k-1)*250+192;
          printf("%lld\n",temp);
      }
      return 0;
}

Bell Number

#To find the nth bell number
a=[]
n=input()
if n==0:
    a.append(1)
else:
    for i in range(1,n+1):
        if len(a)==0:
            a.append(1)
            leng = 1
            start = 0
        else:
            last=a[len(a)-1]
            j=0
            index = start
            while j!=leng:
                element = last+a[index]
                a.append(element)
                index+=1
                j+=1
                last= a[len(a)-1]
            start = len(a) - leng -1   
            leng +=1
print a[len(a)-1]   

Thursday 24 January 2013

10657. LOGIC Problem code: LGIC

import math;a=input();print math.factorial(a)+2**a-a

Perfect Number

////Program to check if a given no. is perfect no.  or not

#include <stdio.h>

/*Function to find the sum of divisors of the number n*/
int sum_of_divisors(n)
{
    int i , sum = 0 ;
   
    for (i=1;i<=n-1;i++)
           if (( n%i )==0)
        sum=sum+i;
    return sum;
}

/*Function to check if the given number is perfect or not by checking the sum of divisors*/
int check_perfect(n)
{
    int sum ;
   
    sum = sum_of_divisors(n);
   
    if (sum==n)
        return 1;
   
    else
        return 0;
}

/*main function*/
int main()
{
    int i , n , check;
   
    printf("enter the no.");
    scanf("%d",&n);

    check = check_perfect(n);
    if (check == 1)
        printf("given no. is perfect no.\n");

    else
        printf ("given no. is not a perfect \n");
}

Wednesday 23 January 2013

2523. Mispelling Problem code: GNY07A

#include<stdio.h>
#include<math.h>
int main()
{
    char c;
    int d,i,j,ct,n;
    scanf("%d",&n);
    ct =1;
    for(j=1;j<=ceil(2*n);j++)
    {
        scanf("%d",&d);
        i=1;
        while((c=getchar())!='\n'&& c!=' ' )
        {
            if (i==1)
                printf("%d ",j/2);
             if (c!=' ' && i!=d)
                printf("%c",c);           
            i++;
        }
            printf("\n");
    }
    return 0;
}

Tuesday 22 January 2013

8371. TRIANGULAR PRISM Problem code: PRISMSA

i=input
p=pow
for t in range(i()):
    v=i()
    a=p(4*v,1.0/3)
    print 3*a*a*p(3,1/2.0)/2

10676. 0110SS Problem code: IWGBS

N=input()
A=[0,1]
B = [0,1]
i = 2
while i<=N:
    A.append(A[i-1]+B[i-1])
    B.append(A[i-1])
    i +=1
print A[N]+B[N]

10657. LOGIC Problem code: LGIC

import math;a=input();print math.factorial(a)+2**a-a

1025. Fashion Shows Problem code: FASHION

#include<cstdio>
#include<iostream>
#include<algorithm>
#define FOR(i,n) for(i=0;i<n;i++)
#define S(N) scanf("%d", &N)
#define ST(a,N) sort(a, a+ N)
using namespace std;
int main()
{
    int test, N, M, ans, i ,men[1001], women[1001];
    S(test);
    while (test--)
    {
         ans = 0;
         S(N);

         FOR(i,N)
                 S(men[i]);

         FOR(i,N)
                 S(women[i]);

         ST(men,N);
         ST(women,N);

         FOR(i,N)
                 ans += men[i] * women[i];
         printf("%d\n", ans);
    }
        return 0;
}

Kamil

#Python code for  53. Kamil Problem code: KAMIL

for i in 10*[0]:s=raw_input();print 2**sum(s.count(i)for i in'DFLT')

Reversed Number

//  C code to reverse the digits of a user entered number
#include<stdio.h>

/*Function to reverse the digits of a number*/
void reverse_digits(n)
{
    while(n!=0)  
    {
        printf("%d",n%10);
        n=n/10;
    }
}
int main()
{
    int n;
    printf("Enter a no.\n");
    scanf("%d",&n);

    printf("The reverse no.is\n");
    reverse_digits(n);
    printf("\n");
}

Perfect Number

////Program to check if a given no. is perfect no.  or not

#include <stdio.h>

/*Function to find the sum of divisors of the number n*/
int sum_of_divisors(n)
{
    int i , sum = 0 ;
   
    for (i=1;i<=n-1;i++)
           if (( n%i )==0)
        sum=sum+i;
    return sum;
}

/*Function to check if the given number is perfect or not by checking the sum of divisors*/
int check_perfect(n)
{
    int sum ;
   
    sum = sum_of_divisors(n);
   
    if (sum==n)
        return 1;
   
    else
        return 0;
}

/*main function*/
int main()
{
    int i , n , check;
   
    printf("enter the no.");
    scanf("%d",&n);

    check = check_perfect(n);
    if (check == 1)
        printf("given no. is perfect no.\n");

    else
        printf ("given no. is not a perfect \n");
}

Centigrate to Fahrenheit

//Program to convert temperature from degree centigrade to Fahrenheit:
// (f-32)/180 = c/100

#include<stdio.h>

int main()
{
    float c,f;

    printf("enter temp in centigrade:\n ");
    scanf("%f",&c);
   
    f=(1.8*c)+32;
    printf("temp in Fahrenheit=\n%f",f);
    return 0;
}

Diamond Shape

/*C code to print a diamond shape when an odd number is entered*/

#include <stdio.h>

/*Function to check whether the number entered is valid or not*/
int check_validity(n)
{
    if (n%2 ==0 || n<=0)
        return 0;
       
    else
        return 1;
}

/*Function to print upper part of the diamond shape for an odd entered number*/
void diamond_upper_shape(n)
{
    int  i , j ,  m ;

    for (i=1 ; i<=n ; i=i+2)
    {       
        for (m=1 ; m<=(n-i)/2 ; m++)
            printf ("  ");
               
        for (j=1;j<=i;j=j+1)           
            printf (" *");                                           

        printf ("\n");
    }       
}

/*Function to print the lower part of the diamond shape for an odd entered number*/
void diamond_lower_shape(n)
{
    int  i , j ,  m ;
   
    for (i=n-2 ; i>=1 ; i=i-2 )
    {   
        for (m=1 ; m<=(n-i)/2  ; m++)
            printf ("  ");
                                       
        for (j=1 ; j<=i ; j=j+1)
            printf (" *");                                           
                                       
        printf ("\n");   
    }

}
/*main function*/
int main()
{
    int  i , j ,  n  , m ;
   
    printf ("enter an odd no.");
    scanf ("%d",&n);
   
    if (check_validity(n)!=0)
    {
        diamond_upper_shape(n);   
        diamond_lower_shape(n);
    }
   
    else
        printf("Enter an odd number\n");
}

Monday 21 January 2013

Pascal Triangle

/*Program to print the element of pascal-triangle when the row and column numbers are given.\

pascal triangle:


                1
             1    1
          1    2    1
       1    3    3    1   
    1    4     6    4    1
  ................................
......................................

*/
#include<stdio.h>

int pascal(int a,int b)
{
    if (a==b)
        return 1;

    else if (b==1)
        return 1;
       
    else 
        return pascal(a-1,b-1)+pascal(a-1,b);
}

int main()
{
    int a,b;
   
    printf("Enter row number : ");
    scanf("%d",&a);
   
    printf("Enter column\n number : ");
    scanf("%d",&b);
   
    if ( (b>a)  ||  (b<=0)  ||  (a<=0)  )
        printf("Invalid Entry\n");
   
    else 
        printf("The (%d,%d)th element of pascal triangle is : %d\n",b,a,pascal(a,b));

   return 0;
}

Fibonacci Levels

#include<stdio.h>

/*Print the levels of fibonacci recursion and also returns the fibonacci value*/
int fib(int n,int level)
{
    int i;

    for(i=0;i<4*level;i++)  //a*level adjusts the horizontal space between two levels
        printf(" ");

    printf("fib(%d)\n",n);

    if(n==0)
        return 0;

    else if(n==1)
        return 1;

    else 
        return (fib(n-1,++level))+(fib(n-2,level));    //fib(n) = fib(n-1)+fib(n-2) ; n>=2
}

//main function
int main()
{
    int n , f;
   
    printf ("Enter the no. whose fibonacci no. is to be found;  ");
    scanf("%d",&n);
    
    printf("fib(%d)=%d\n", n , fib(n , 0) );
 
   return 0;
}

Matrix Rotation

// Program to rotate the nxn matrix to the right by 90 degree..
#include<stdio.h>
#define max 100

/*Function to store  the matrix*/
void read_matrix(int a[][max] , int order)
{
    int i , j , n = order-1;
    for (i=0;i<=n;i++)
        for (j=0;j<=n;j++)
           scanf("%d",&a[i][j]);
}


/*Function to rotate a matrix to the right by 90 degress*/
void rotate_right( int a[][max] , int b[][max] , int order)
{
    int i , j , n = order -1;
    for (i=0;i<=n;i++)
        for (j=0;j<=n;j++)   
             b[j][n-i]=a[i][j];
}

/*Function to print the matrix*/
void print_matrix(int a[][max] , int order)
{
    int i  , j , n =  order-1;
    for (i=0;i<=n;i++)
    {
        for (j=0;j<=n;j++)
          printf("%d\t",a[i][j]);
       
        printf("\n");
    }   
}

/*main function*/
int main()
{
    int  i , j , n ;
    int a[max][max];
    int b[max][max];
   
    printf("enter the order of matrix\n");
    scanf ("%d",&n);

    printf ("enter the nxn matrix\n");
    read_matrix (a , n);
    rotate_right( a , b , n);

    printf ("before rotation: \n");
    print_matrix( a , n);
    printf ("\n");

    printf ("after rotation: \n");
    print_matrix(b , n);
    printf("\n");
   
    return 0;
}

Circular Prime

 /* C code to check whether a number obtained by circularly shifting its digits is prime or not*/
#include <stdio.h>
//function for finding the number of digits in a given number
int get_num_digits(n)
{
    int k = 0;
    while (n!=0)
    {
        n = n/10;
        k++;
    }
    return k;
}

//function for finding a circular number
int get_shifted_num(n)
{

    int i , d, k = 1 , last_digit , new_num;

    d= get_num_digits(n);

    for (i=1 ; i<=(d-1); i++)
        k = k*10;

    last_digit = n/k;

    new_num = (n - last_digit*k)*10 + last_digit;
    return new_num;
}

//function to check whether a number is prime or not
 is_prime(n)
{
    int  i , circular_num =n;   
    for (i=1; i<=circular_num/2 ; i++)
        if (circular_num%(i+1) == 0)
                return  0 ;
    return 1;           
}

int main ()
{
    int i , n , d ,  circular_num , z;

    printf("enter a number : ");
    scanf("%d", &n);

    d = get_num_digits(n);
   
    circular_num =n;

    for (i = 1; i<=d ; i++)
    {
        printf ("shifted no. is  : %d\n", circular_num);
        z = is_prime(circular_num);

        if (z==1)
            printf ("%d is prime\n\n", circular_num);
        else
            printf ("%d is not prime: \n\n" , circular_num);

        circular_num = get_shifted_num(circular_num);
    }
    return 0;
}

To and Fro

400. To and Fro Problem code: TOANDFRO

 #include<iostream>
 using namespace std;
 int main()
 {
     int columns,n,m;
     while (1)
     {
            cin>>columns;
            if (columns==0)
                break;
         string s;
         cin >> s;
         string r(s);

         int rows = s.size() / columns;
         for (int i=0; i<s.size(); i++)
         {
            m = i/columns;
            m%2==0?n=i%columns:n = columns-1-i%columns;   
            r[n*rows+m] = s[i];
         }
         cout << r << endl;
     }
     return 0;
 }

1724. Counting Triangles Problem code: TRICOUNT

#include<stdio.h>
int main()
{
        long long int test,n;
        scanf("%lld",&test);
        while(test--)
            scanf("%lld",&n)&&printf("%lld\n",n*(n+2)*(2*n+1)/8);
        return 0;
}

9948. Will it ever stop Problem code: WILLITST

#include<stdio.h>
int main()
{
    long long int  n;
    while(scanf("%lld",&n)!=EOF)
    {
        if((n&(n-1))==0)
            printf("TAK\n");
        else
            printf("NIE\n");
    }
    return 0;
}

2716. Maximal Quadrilateral Area Problem code: QUADAREA

#include<cstdio>
#include<iostream>
#include<math.h>
int main()
{
    int test;
    double a,b,c,d,s,max_area;
    scanf("%d",&test);
    while(test--)
    {
        scanf("%lf %lf %lf %lf",&a,&b,&c,&d);
        s=(a+b+c+d)/2;
        max_area=sqrt(((s-a)*(s-b)*(s-c)*(s-d)));
        printf("%0.2lf\n",max_area);
    }
    return 0;
}

Sum the Series

11746. Sum the Series Problem code: SUMUP

for j in[1]*input():
    s,a,b=0,1,3
    for i in range(input()):s+=(b-a)/(b*a*2.0);a=b;b+=2*i+4
    print"{0:.5f}".format(s)

3442. The last digit Problem code: LASTDIG

#include<cmath>
#include<cstdio>
int main()
{
    long long int a , b , n, i , k;
    scanf("%lld", &n);
    for (i=0;i<n;i++)
    {
        scanf("%lld %lld", &a , &b);
               
        if(b==0&&a==0)
            k=1;
        else if (b==0 )
            k=1;
        else if (a==0)
            k=0;
        else
        {                   
            if (b%4==0)
                b = 4;
            else
                b=b%4;
            k = pow(a, b);
        }   
            k = k%10;   
            printf("%lld\n", k);       
    }
    return 0;
}

302. Count on Cantor Problem code: CANTON

#include<stdio.h>
#include<math.h>
int main()
{
    long long int n , x ,i , j ,sub , sum , t;
   
    scanf("%lld",&t);
    while(t--)
    {
        scanf("%lld",&x);
        n=ceil((-1+sqrt(1+8*x))/2);
        sub = x - n*(n-1)/2;
        sum = n+1;
        if (n%2==0)
            printf("TERM %lld IS %lld/%lld\n",x , sub , sum-sub);   
        else
            printf("TERM %lld IS %lld/%lld\n",x, sum -sub ,sub);           
    }
    return 0;
}

2123. Candy I Problem code: CANDY

#include<stdio.h>
int main()
{
    int n, k,avg,sum,count;
    while(1)
    {
        scanf("%d",&n);

        if(n==-1)
            break;

        int a[n+1];   
        sum=0;   
        count=0;

        for(k=0;k<n;k++)       
        {
            scanf("%d",&a[k]);
            sum=sum+a[k];
        }   
            avg=sum/n;
            if(avg*n!=sum)
                printf("-1\n");
            else
            {
                for(k=0;k<n;k++)
                    if(a[k]<avg)
                        count=count+avg-a[k];
                printf("%d\n",count);           
            }   
           
    }
    return 0;
}

10286. DOTA HEROES Problem code: DOTAA

#include<stdio.h>
int main()
{
    int t,n,m,D,count,i,H;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&D);
        count=0;
        while(n--)
        {
            scanf("%d",&H);
            if (H>D)
            {
                while(H>0)
                {
                    H=H-D;
                    count++;
                }
                count--;
            }
        }
        if (count<m)
            printf("NO\n");
        else
            printf("YES\n");   
    }
    return 0;
}

11354. Amusing numbers Problem code: TSHOW1

#include<stdio.h>
#include<math.h>
int main()
{
    long long int x,y,sum;
    int n ,k , t, a[51],i=0,j;
    scanf("%d",&t);
    while(t--)
    {
    scanf("%lld",&x);
    i=0;
    n = ceil(log2((x+3)/2));
    sum = 2*(pow(2,n-1)-1);
    y = x -sum-1;
    while(1)
    {
        a[i++]=y%2;       
        y=y/2;               
        if(y==0)
        {
            k= n-i;
            if(i!=n)
               
                while(k--)
                    a[i++]=0;    ;               
            break;
           
        }
    }
    for(j=i-1;j>=0;j--)
        if(a[j]==0)       
            printf("5");   
        else if(a[j]==1)
            printf("6");
    printf("\n");
    }   
    return 0;
}

1002. Uncle Jack Problem code: UJ

while 1:
 n,s,d=raw_input().partition(' ')
 if n=="0" and d=="0":
  break
 else:
  print (int(n)**(int(d)))

Factorial

11. Factorial Problem code: FCTRL

def f(n): 
    if n == 0: 
        return 1 
        else: 
            return n * f(n-1) 
i=input     
for t in range(i()): 
    print "%d" % f(i()) 

Life, the Universe, and Everything

1. Life, the Universe, and Everything Problem code: TEST


#include <stdio.h>
int main()
{
    int N;
    while( 1 )
    {
        scanf( "%d", &N );
   
        if( N == 42 )
                break;
       
        printf( "%d\n", N );
    }
    return 0;
}

Feynman

3410. Feynman Problem code: SAMER08F

#include<stdio.h>
int main()
{
    int n;
       while(1)
       {
              scanf("%d",&n);
              if(n==0)
                  break;
              printf("%d\n", (n*(n+1)*(2*n+1))/6 );
        }
        return 0;
}

Family Problems

5297. Family Problems Problem code: FAMILYP

#include<stdio.h>
#include<math.h>
int main()
{
    long long int N;
    int ans;
    float n;
    char c;
       
       while((scanf("%lld",&N)) !=EOF)
       {
            n=-1+sqrt(1+8*N);
            n=n/2;
       
            long k;
            k=ceil(n);
          ans=k%27-1;
          printf("TERM %lld IS %c\n",N,65+ans);
         
        }
        return 0;
}

Transform the Expression

4. Transform the Expression Problem code: ONP

#include<stdio.h>
#include<ctype.h>
char stack[1000];
int top;

void initialize_stack()
{
    top=-1;
}

char pop()
{
    return stack[top--];
}

void push(char x)
{
    stack[++top]=x;
}

int precedence(char op)
{
    if (op=='^')
        return 3;
    if((op=='*')||(op=='/'))
        return 2;
  
    else if ((op=='+')||(op=='-'))
        return 1;
}

int greater_or_equal( char op1 , char op2 )
{
    return(precedence(op1)>=precedence(op2));
}

int is_operator(char c)
{
    return (c=='*')||(c=='/')||(c=='+')||(c=='-' ||c=='^');
}

int main()
{
    int in;
    char c;
  
    in=0;
    initialize_stack();
    int t;
    scanf("%d\n",&t);
    while (t--)
    {
    while((c=getchar())!='\n')
    {
        if ( (isalpha(c)) ||(c=='.'))
        {
            printf("%c",c);
        }
           
         else if(is_operator(c))
         {
              while( (top!=-1) && (stack[top]!='(') && (greater_or_equal(stack[top],c)) )
                    printf("%c",pop());
                    
              push(c);
          }
                
         else if(c=='(')
             push(c);
                
          else if(c==')')
         { 
              while(stack[top]!='(')
                    printf("%c",pop());
                    top--;
          }
       
      }

    while(top!=-1)
        printf("%c",pop());
          printf("\n");
        }
}

Julka

54. Julka Problem code: JULKA

j=input
for i in range(10):
    n=j()
    x=j()
    k = (n-x)/2
    print k+x
    print k

Hangover

902. Hangover Problem code: HANGOVER

#include<stdio.h>
/*finding the minimum no. of cards for at least c overhang*/
int overhang(float c)
{
    float sum = 0;
    float k=2;
   
    while (sum < c)
    {
            sum =  sum + 1/k;
            k++;
    }

    return k-2;   
}

/*main function*/
int main()
{
    float c;
    int cards;
    scanf("%f" , &c);
    while(c!=0.00 && c< 10.00)
    {
        cards =    overhang(c);
        printf("%d card(s)\n", cards);
        scanf("%f" , &c);
    }   
    return 0;
}

Rectangles

4300. Rectangles Problem code: AE00

#include<stdio.h>
int main()
{
    long long int n,i,j,count=0;
    scanf("%lld",&n);
   
    for(i=1;i<=n;i++)
    {
        for (j=i;j<=n;j++)
        {
            if(i*j>n)
                break;
            count++;   
        }
    }
    printf("%lld\n",count);
    return 0;
}

What's Next

 7974. What’s Next Problem code: ACPC10A

#include<stdio.h>
int main()
{
    int a , b ,c ;
    while(1)
    {   
        scanf("%d %d %d", &a,&b,&c);
        if (a==0 && b==0 && c==0)
            break;
       
        if (b-a == c-b)
            printf("AP %d\n",c+(b-a));
       
        else if(b/a == c/b)
            printf("GP %d\n",c*(b/a));           
    }
    return 0;
}

Girls and Boys

7424. Girls and Boys Problem code: GIRLSNBS

#include<stdio.h>
int min(int a , int b)
{
    return a<b?a:b;
}

int max(int a , int b)
{
    return a>b?a:b;
}
int main()
{
    int g , b , c ,d,e,f,ans;
   
    while(1)
    {
        scanf("%d%d",&b ,&g);
        if(b==-1&&g==-1)
            break;
       
        else
        {
            c=min(b,g);
            d=max(b,g);
            e=d/(c+1);
            f=d%(c+1);
            if(f==0)
                ans=e;
            else
                ans =e+1;               
            printf("%d\n",ans);                                   
        }
    }   
    return 0;
}

Number Steps

1112. Number Steps Problem code: NSTEPS

#include<stdio.h>
int main()
{
    int n , i , x, y;
    scanf("%d", &n);
   
    for (i = 0 ; i< n ;i++)
    {
        scanf("%d", &x);
        scanf("%d" , &y);

        if (x%2==0 && y%2==0 && (x-y ==2 ||  x-y == 0 ))
            printf("%d\n" , x+y);
   
   
        else if ((x+1)%2==0 && (y+1)%2==0 && (x-y ==2 ||  x-y == 0 ))
            printf("%d\n" , x+y-1);
       
        else
            printf("No Number\n");           
    }
        return 0;
}

Numeral System

1699. Numeral System Problem code: NSYSTEM

def convert(s):
    sum=0
    init =1;
    for i in s:
        if i.isalpha()==False:
            if i=='2':
                init = init*2;
            elif i=='3':
                init = init*3;
            elif i=='4':
                init = init*4;           
            elif i=='5':
                init = init*5;           
            elif i=='6':
                init = init*6;           
            elif i=='7':
                init = init*7;   
            elif i=='8':
                init = init*8;               
            elif i=='9':
                init = init*9;   
               
        if i.isalpha():
            if i=='m':
                init = init*1000
            elif i=='c':
                init =init*100
            elif i=='x':
                init =init*10
            elif i=='i':
                init =init*1
            sum = sum+init
            init =1
    return sum                                       
                   
def convert_back(s):
    r =s;
    k=1
    while r!=0:
        r =r/10
        k=k*10
    k =k/10
    r=s
    q =""
    while k!=0:
        r =s
        r =r%(k*10)
        t =r//k
           
        if t!=1 and t!=0:
            q = q+str(t)

        if k==1000 and t!=0:
            q = q+'m'
        elif k==100 and t!=0:
            q = q+'c'
        elif k==10 and t!=0:
            q = q+'x'
        elif k==1 and t!=0:
            q = q+'i'                                               
        k=k/10                   
    print q                   

for i in range(0, input()):
    s = raw_input()
    tag=0
    m1 = ""
    m2= ""
    for c in s:
        if c==' ':
            tag=1
            continue
        elif tag==0:
            m1 = m1+c
       
        else:
            m2 = m2+c

    a =convert(m1)                   
    b =convert(m2)
    c = a+b
    convert_back(c)   

Guess the Number

7190. Guess the Number Problem code: GUESSTHE

#include<stdio.h>

long long int gcd(long long int a, long long int b)
{
    if (a<b)
        return gcd(b,a);

    if (b==0)
        return a;

    else
            return gcd (b,a%b);
}

long long int lcm(long long int a,long long int b)
{
    return (a*b)/gcd(a,b);
}   

int main()
{
    char c;
    long long int count, k, i ,j;
   
    while(1)
    {
        count=0;
        k=1;
        i = 0;
        int toCheck[21];
        scanf("%c",&c);
        while(c!='\n' && c!='*')
       
        {
            count++;
                   
            if (c=='Y')
            {
                k= lcm(k,count);
            }
            else if(c=='N')
            {
                toCheck[i++]  = count;       
            }   
            scanf("%c",&c);
               
        }
       
        for(j = 0 ; j< i ; j++)
        {
            if(k%toCheck[j]==0)
                {
                    k=-1;
                    break;
                }
        }
        if(c=='*')
            break;
        printf("%lld\n",k);
       
    }
    return 0;
}

Adding Reversed Numbers

42. Adding Reversed Numbers Problem code: ADDREV

#include<stdio.h>

int reverse_number(int n)
{
    int r=0, a;
    while(n>=1)
    {
        a=n%10;
        r=r*10+a;
        n=n/10;
    }
    return r;
}

int main()
{
    int n , i , x , y;
    int r , s , t;
    scanf("%d", &n);

    for (i=0 ; i<n; i++)
    {
        scanf("%d", &x);
        scanf("%d", &y);
       
        r = reverse_number(x);
        s = reverse_number(y);
   
        t = reverse_number(r+s);
        printf("%d\n", t );
    }
    return 0;
}

Beehive Numbers

7406. Beehive Numbers Problem code: BEENUMS

#include<stdio.h>
#include<math.h>
int main()
{
    long long int n,d,i;
      scanf("%lld",&n);
    float k;
       
    while(n!=-1)
    {
        k=(-3+sqrt(-3+12*n))/6;
        d=k;
        if(d-k==0)
            printf("Y\n");
        else
            printf("N\n");   

        scanf("%lld",&n);   
    }       
        return 0;
}

Polybius Square

5842. Polybius square Problem code: POLYBIUS

for j in[0]*input():
    for c in raw_input():
         if c!=' ':a=ord(c)-65-(c>'I');print a/5*10+a%5+11,
    print
Read more...

MAGIC SQUARE

 //C code to print magic square for odd numbers entered
#include<stdio.h>
/*function to get number of digits*/
int get_num_of_digits(n)
{
    int k = 0 ;
    while (n!=0)
    {
        n/=10;
        k++;
    }
    return k;
}

/* move up */
int up ( int n ,  int i)
{
    return i==0?n-1:--i;
   
}

/*move left */
int left ( int n , int j)
{
    return j==0?n-1:--j ;       
}

/*To print magic square*/
void print_magic(  int n , int a[][n])
{
    int b , c , d, i  , g;
   
     d = get_num_of_digits(n*n);
    
    for(b = 0 ; b< n ; b++)
    {
        for (c= 0 ; c<n ; c++)
        {   
            g =  get_num_of_digits(a[b][c]);
               
            for ( i = 0; i<d-g ; i++)
                printf(" ");
       
            printf("%d " , a[b][c]);
        }
        printf("\n");   
    }
}

/* to store magic elements*/
int store_magic_element(int n , int i , int j , int k , int a[][n])
{
    a[i][j] = k++;
    return k;
}

/*Function to get magic square*/
void get_magic_square(int n , int a[][n])
{
    int  i= 0  , j = n/2 ;
    int count = 0 , temp ,  k = 1;
   
    while ( count!= n)
    {
        k = store_magic_element(n ,  i , j , k , a);
        temp = 1;
       
        while(temp!=n)
        {
            i = up(n , i);       
            j = left(n , j);                   
            k = store_magic_element(n ,  i , j , k , a);
            temp++;
        }
        count++;
        i++;   
    }
}

/*main function*/
int main()
{
    int n ;
   
    printf("enter the order of matrix : ");
    scanf("%d" , &n);   
   
    int a[n][n];
   
    get_magic_square(n , a);
    print_magic( n , a);       
}