20 Mar 2016

Naughty Sid and SEV


/*

Sid is a very naughty, plump and food-loving boy. Today, his mother has made the special snack SEV. She fills this SEV up to a height ‘H’ cm in a transparent cubical closed box of edge ‘a’ cm (Fig 1) . When Sid asks mother for the SEV, she gives him a little and tells him: ”Only this much for today.” 
 
However Sid is not satisfied. 

He waits for his mother to go out shopping and sneaks into the kitchen. He knows that his mother will scold him if she discovers that he has eaten the SEV without her permission. However, he just can't resist the temptation. He suddenly gets an idea : “If I eat the SEV up to a height ‘h’ from bottom (ie. 'h' height of SEV will be left in the box after he's done eating Fig.2), and then reorganize the SEV (as shown in (Fig.3)), Mom won’t realize that I have eaten the SEV (as Mom sees the SEV only from FRONT VIEW)!“. Unfortunately for Sid , the SEV can stay stable only up to at a maximum sloping angle of THETA_MAX degrees (acute angle) with respect to the ground (horizontal). Help Sid by telling him the maximum amount of SEV he can eat by telling him the appropriate height ‘h’ from bottom up to which he can eat the SEV.
enter image description here

Input:
First line consisting of t, the number of test cases. Each test case comprising of a single line containing space separated integers a, H and THETA_MAX.

Output:
A single line for every test case, consisting of the value of the smallest integer greater than or equal h.

Constraints:
1<=t<=100000
20 <= a <= 100,
0 < H <= a,
0<= THETA_MAX ( in degrees ) < 90.
 
*/


#include <stdio.h>
#include <math.h>
#define PI 3.141593


int main()
{   
    int t,a,H,THETA_MAX,i,h;
    float vol_temp,b_temp,h_temp;


    scanf("%d",&t);
    for(i=0;i<t;i++){
        scanf("%d %d %d",&a,&H,&THETA_MAX);
       
        if(THETA_MAX==0) h=H;
       
        else if(H/tan(THETA_MAX*PI/180)>a){
           
            vol_temp=0.5*H*H/tan(THETA_MAX*PI/180);
            b_temp=H/tan(THETA_MAX*PI/180)-a;
            h_temp=tan(THETA_MAX*PI/180)*b_temp;
            h=ceil((vol_temp-0.5*b_temp*h_temp)/a);
        }
        else {
           
            vol_temp=0.5*H*H/tan(THETA_MAX*PI/180);
            h=ceil(vol_temp/a);
        }

        printf("%d\n",h);
    }
    return 0;
}



Categories: , ,

Wobbly Numbers

/*
An N-length wobbly number is of the form "ababababab..." and so on of length N, where a!=b.

Find Kth wobbly number from a lexicographically sorted list of N-length wobbly numbers. If the number does not exist print 1 else print the Kth wobbly number.

Input:
First line contains T - number of test cases
Each of the next T lines contains two space separated integers - N and K


Output:
For each test case print the required output in a new line. 


Constraints: 1T100
3N1000
1K100

*/ 

#include <stdio.h>

int main()
{
    int t,i,n,k,a,b,j,temp;

    scanf("%d",&t);
    for(i=0;i<t;i++){
        scanf("%d %d",&n,&k);
       
        if(k>81)
            printf("-1\n");
        else{
            a=ceil((double)k/9.0);
            temp=k-9*(a-1);
            b=(temp<=a)?temp-1:temp;
            for(j=0;j<n;j++)
                if(j%2==0) printf("%d",a);
                else printf("%d",b);
            printf("\n");
        }
    }

}

Categories: , ,

Hungry Lemurs

/*
//JAVA
 
There are K lemurs on Madagascar and Kevin has N bananas. He has to give away all his bananas to lemurs. Lemurs are happy if all of them get the same number of bananas (even if they don't get any). In one minute Kevin can do one of the following:
  • Find one banana.
  • Discard one banana (eat).
  • Increase a population of lemurs on Madagascar by one.
  • If there are at least two lemurs remove one of them from Madagascar (don't ask how).
Help Kevin and find the minimum number of minutes Kevin needs to satisfy all the lemurs.

Input format:
The only line of the input contains two space-separated integers N and K.

Output format:
Output the single integer -- the minimum number of minutes.

Constraints:
  • 1 ≤ K,N ≤ 105
  • K, N ≤ 50 in test data worth 33% of all points
 */


import java.io.BufferedReader;
import java.io.InputStreamReader;

class HungryLemurs{
public static void main(String args[] ) throws Exception {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String[] inputs = br.readLine().split("\\s+");
    int bananas = Integer.parseInt(inputs[0]);
    int lemurs = Integer.parseInt(inputs[1]);

    if (bananas <= lemurs) {
        int totalMinutes = (int)Math.min(bananas, lemurs - bananas);
        System.out.println(totalMinutes);
    }
    else {

        int bestMinutes = Integer.MAX_VALUE;

        for (int k = 1; k <= bananas; k++) {
            int totalMinutes = (int)Math.abs(lemurs - k);
            int multipleBefore = (bananas / k) * k;
            int multipleAfter = multipleBefore + k;
            totalMinutes += (int)(Math.min(bananas - multipleBefore, multipleAfter - bananas));

            if (totalMinutes < bestMinutes)
                    bestMinutes = totalMinutes;

        }

        System.out.println(bestMinutes);
    }
}
}

Categories: , ,

17 Mar 2016

Clock angle problem


/*

Find minimum angle between hour and minute hand. 

Input:
The first line contains the number of test cases, T. T lines follow, each of which contains two integer Hour hand H and minute hand M .

Output:
Print the minimum angle between the hands.
Constraints
1<=T<=100
01<=H<=12
01<=M<=59

*/ 

#include <stdio.h>

int main()
{
    int T,*h,*m,h_angle,m_angle,angle,i;
   
    scanf("%d",&T);
    h=(int*)malloc(T*sizeof(int));
    m=(int*)malloc(T*sizeof(int));
   
    for(i=0;i<T;i++){
       
        scanf("%d %d",&h[i],&m[i]);   
        if (h[i] == 12) h[i] = 0;
        if (m[i] == 60) m[i] = 0;
       
        h_angle=0.5 * (h[i]*60 + m[i]);
        m_angle=6*m[i];
        angle=abs(h_angle-m_angle);
       
        if(360-angle <angle) angle=360-angle;
       
        printf("%d\n",angle);
    }
   
   
    return 0;
}


Categories: , , , ,

Trailing Zeroes


/*

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

*/

#include <stdio.h>

int main(){
   
    int i,N,z,k;
    scanf("%d",&N);
   
    z=0;
    k=1;
    i=1;
    while(1){
       
        k=(int)(N/pow(5,i));
        if(k<1) break;
        z+=k;
        i++;
    }
   
    printf("%d",z);
    return 0;
}

Categories: , , ,

Copyright © UPgradeCODING | Powered by Blogger