Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

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: 1≤T≤100
3≤N≤1000
1≤K≤100

*/ 

#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: , ,

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: , , ,

13 Apr 2013

Command Line arguments

//Command Line Arguments 
 
#include <stdio.h>

int main ( int argc, char *argv[] )
{
    if ( argc != 2 ) /* argc should be 2 for correct execution */
    {
        /* We print argv[0] assuming it is the program name */
        printf( "usage: %s filename", argv[0] );
    }
    else 
    {
        // We assume argv[1] is a filename to open
        FILE *file = fopen( argv[1], "r" );

        /* fopen returns 0, the NULL pointer, on failure */
        if ( file == 0 )
        {
            printf( "Could not open file\n" );
        }
        else 
        {
            int x;
            /* read one character at a time from file, stopping at EOF, which
               indicates the end of the file.  Note that the idiom of "assign
               to a variable, check the value" used below works because
               the assignment statement evaluates to the value assigned. */
            while  ( ( x = fgetc( file ) ) != EOF )
            {
                printf( "%c", x );
            }
            fclose( file );
        }
    }
 
 
TO KNOW mORE....http://courses.cms.caltech.edu/cs11/material/c/mike/misc/cmdline_args.html 

Categories: , , , ,

17 Mar 2013

Parallel Computing_SOLVED

/*

CODECHEF
PROBLEM CODE : PARALLEL

*/

#include<stdio.h>

int main()
{
int a,b,steps,n;
scanf("%d",&n);
for(b=n,steps=0;b;b/=2)
steps++;
steps=(steps-1)*2;
printf("%d\n",steps);
for(b=2;b<=n;b*=2)
{
printf("%d",n/b);
for(a=b;a<=n;a+=b)
printf(" %d+%d=%d",a-b/2,a,a);
printf("\n");
}
for(b/=2;b>=2;b/=2)
{
printf("%d",(n-b/2)/b);
for(a=b+b/2;a<=n;a+=b)
printf(" %d+%d=%d",a-b/2,a,a);
printf("\n");
}
return 0;
}

Categories: , , ,

Bonus_SOLVED


/*

CODECHEF
PROBLEM CODE : ACMKANPB

*/

#include <stdio.h>
 
int main()
{
int fall, i, c, n, m, l, t, j, x[4][10100];

for(scanf("%d",&fall); fall--;)
{
for(i=!!scanf("%d %d %d",&n,&m,&l); i<=m; x[0][i++]=l);
for(i=-1; ++i<m; scanf("%d %d %d",&x[1][i],&x[2][i],&x[3][i]));
for(j=!(c=1); c&&j<n; j++)
for(i=-!(c=0); ++i<m; x[0][x[1][i]]=(x[0][x[1][i]]<x[0][x[2][i]]+x[3][i])?x[0][x[2][i]]+x[3][i]+(c=1)*0:x[0][x[1][i]]);
for(i=0; x[0][x[1][i]]-x[0][x[2][i]]>=x[3][i]&&i<m; i++);
if(i<m)
puts("Inconsistent analysis.");
else
{
for(i=!(t=0); i<=n; t+=x[0][i++]);
printf("%d\n",t);

for(i=1; i<=n; printf("%d ",x[0][i++]));
puts("");
}
}
return 0;
}

Categories: , , ,

Logging Game_SOLVED


/*

CODECHEF
PROBLEM CODE : LOGGERS

*/

#include<stdio.h>
 
 
int sgtable[251]={-1,0,1,2,3,1,4,3,2,1,4,2,6,4,1,2,7,1,4,3,2,1,4,6,7
,4,1,2,8,5,4,7,2,1,8,6,7,4,1,2,3,1,4,7,2,1,8,2,7,4,1,2,8,1,4,7,2,1,
4,2,7,4,1,2,8,1,4,7,2,1,8,6,7,4,1,2,8,1,4,7,2,1,8,2,7,4,1,2,8,1,4,7,2,1,8,2,7,4,1,2,
8,1,4,7,2,1,8,2,7,4,1,2,8,1,4,7,2,1,8,2,7,4,1,2,8,1,4,7,2,1,8,2,7
,4,1,2,8,1,4,7,2,1,8,2,7,4,1,2,8,1,4,7,2,1,8,2,7,4,1,2,8,1,4,7,2,1,
8,2,7,4,1,2,8,1,4,7,2,1,8,2,7,4,1,2,8,1,4,7,2,1,8,2,7,4,1
,2,8,1,4,7,2,1,8,2,7,4,1,2,8,1,4,7,2,1,8,2,7,4,1,2,8,1,4
,7,2,1,8,2,7,4,1,2,8,1,4,7,2,1,8,2,7,4,1,2,8,1,4,7,2,1,8};
 
int main()
{
 
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int i,val,ans=0;
for(i=0;i<n;i++)
{
scanf("%d",&val);;
ans=ans^sgtable[val];
}
if(ans)printf("Alice\n");
else printf("Bob\n");
}
return 0;
}

Categories: , , ,

Copyright © UPgradeCODING | Powered by Blogger