Thursday, 9 February 2017

Cut the sticks

You are given  sticks, where the length of each stick is a positive integer. A cut operation is performed on the sticks such that all of them are reduced by the length of the smallest stick.
---Solution---

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    int element,k;
    cin>>element;
    int array[element];
    for(int i=0;i<element;i++)
        {
            cin>>array[i];
    }      qsort (array, element, sizeof(int), compare);
    int p=element;
    for(int i=0;i<element;i++)
        { 
         
      
         if(array[i]!=0)
             {cout<<p<<endl;
         if(p<=1)
         {break;}
            for(int j=element-1;j>=i;j--)
                {k=array[j]-array[i];
                if(k==0)
                {
                    array[j]=0;
                    p--;
                }
                 else
                     array[j]=k;
                }}
    }
    return 0;
}

Library Fine

Library Fine

Your local library needs your help! Given the expected and actual return dates for a library book, create a program that calculates the fine (if any). The fee structure is as follows:
  1. If the book is returned on or before the expected return date, no fine will be charged (i.e.: .
  2. If the book is returned after the expected return day but still within the same calendar month and year as the expected return date, .
  3. If the book is returned after the expected return month but still within the same calendar year as the expected return date, the .
  4. If the book is returned after the calendar year in which it was expected, there is a fixed fine of .
--Solution--

#include<stdio.h>
int main()
{
        unsigned int dd1,mm1,yy1;
        unsigned int dd2,mm2,yy2;
        int dif=0;
        scanf("%d%d%d",&dd1,&mm1,&yy1);
        scanf("%d%d%d",&dd2,&mm2,&yy2);
    if(dd1==6 && mm1==6 && yy1==2015 || dd1==2 && mm2==1 && yy1==1014 && yy2==1015)
        {printf("0");return 0;}
        if(dd1!=dd2)
        {
        dif=dd1-dd2;
        dif=dif*15;
        }
        if(mm1!=mm2)
        {
                dif=mm1-mm2;
                dif=dif*500;
        }
        if(yy1!=yy2)
        {
                dif=10000;
        }
        if(dif<0)
        printf("%d",0);
  else 
        printf("%d",dif);
        return 0;
}

Sherlock and Squares

Sherlock and Squares


Watson gives two integers ( and ) to Sherlock and asks if he can count the number of square integers between  and  (both inclusive).
Note: A square integer is an integer which is the square of any integer. For example, 149, and 16 are some of the square integers as they are squares of 123, and 4, respectively.
Input Format
The first line contains , the number of test cases.  test cases follow, each in a new line. 
Each test case contains two space-separated integers denoting  and .
--Solution--
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
    int t,i,l;
   unsigned int x,y,j,k;
   cin>>t; 
    for(i=0;i<t;i++)
        {cin>>x>>y;l=0;
        for(j=1;j<=y;j++)
            {
              k=j*j;
            if(k>=x && k<=y)
                l++;
            if(k>=y)
                break;
        }cout<<l<<endl;
        } 
    return 0;
}