Tuesday, 5 April 2016

Competitve Programming : Dealing With If.

Minimum dimension of the picture can be L x L, where L is the length of the side of square.
Now Roy has N photos of various dimensions.
Dimension of a photo is denoted as W x H
where W - width of the photo and H - Height of the photo
When any photo is uploaded following events may occur:
[1] If any of the width or height is less than L, user is prompted to upload another one. Print "UPLOAD ANOTHER" in this case.
[2] If width and height, both are large enough and
(a) if the photo is already square then it is accepted. Print "ACCEPTED" in this case.
(b) else user is prompted to crop it. Print "CROP IT" in this case.
(quotes are only for clarification)
Given L, N, W and H as input, print appropriate text as output.
Input:
First line contains L.
Second line contains N, number of photos.
Following N lines each contains two space separated integers W and H.
Output:
Print appropriate text for each photo in a new line.
Constraints:
1 <= L,W,H <= 10000
1 <= N <= 1000


Source Code :


#include<conio.h>
#inlcude<stdio.h>
int main()
{
int L,W[1000],H[1000],N,i;
 scanf("%d",&L); 
 scanf("%d",&N);
 for(i=0;i<N;i++) 
 {
scanf("%d%d",&W[i],&H[i]);
}                        
 for(i=0;i<N;i++)
 {
if(W[i]>=L&&H[i]>=L)
 {
if(W[i]*H[i]<=(L*L))
{
printf("\nACCEPTED ");
}
else if((W[i]==H[i]))
{
printf("\nACCEPTED ");
}
else if((W[i]*H[i]>L*L))
{
printf("\nCROP IT");
}
}
else if(W[i]<L||H[i]<L)
{
printf("\nUPLOAD ANOTHER ");
}        
}

}


PS : Here TO avoid Dangling if else problem We have used Nested if.

Friday, 25 March 2016

Competitve Programming - Trailing Zeores

Given a number find the number of trailing zeroes in its factorial.
Input Format
A single integer - N
Output Format
Print a single integer which is the number of trailing zeroes.
Input Constraints
1 <= N <= 1000

Sample Input : 10
Output : 2 

Explanation : 10! = 3628800 has 2 zeros in the end.


                                                 Source Code
#include<conio.h>
#include<stdio.h>
main()
{
int i,j,n,count=0;
long long int fact;
scanf("%d",&n);
for(j=5;j<=n;j*=5)
{
fact=1;
for(i=1;i<=n;i++)
{
fact*=i;
if(i%j==0)
{
count++;
}
}
}
printf("%d",count);
getch();
}

Logic Applied :  Let us Take an Example of 23!



    I know that a number gets a zero at the end of it if the number has 10 as a factor. For instance, 10 is a factor of 50,120, and  1234567890. So I need to find out how many times 10 is a factor in the expansion of 23!.
    But since 5×2 = 10, I need to account for all the products of 5 and 2. Looking at the factors in the above expansion, there are many more numbers that are multiples of 2 (2, 4, 6, 8, 10, 12, 14,...)than are multiples of 5 (5, 10, 15,...). That is, if I take all the numbers with 5as a factor, I'll have way more than enough even numbers to pair with them to get factors of 10 (and another trailing zero on my factorial). So to find the number of times 10is a factor, all I really need to worry about is how many times 5 is a factor in all of the numbers between 1 and 23.
    How many multiples of 5 are between 1 and 23? There is 51015, and 20, for four multiples of 5. Paired with 2's from the even factors, this makes for four factors of 10, so:
                                23! has four trailing zeroes