Showing posts with label competitive programming. Show all posts
Showing posts with label competitive programming. Show all posts

20 Mar 2016

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

Copyright © UPgradeCODING | Powered by Blogger