Thief trying to escape

PROBLEM :

A thief trying to escape from a jail has to cross N walls each with varying heights. He climbs X feet every time. But, due to the slippery nature of those walls, every times he slips back by Y feet.  Now the task is to calculate the total number of jumps required to cross all walls and escape from the jail.

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains two space separated integers X, Y, N. Then in the next line are N space separated values denoting the heights ( Ht[] ) of the walls.

Output:
For each test case in a new line print the total number of jumps.

Constraints:
1<=T<=100
1<= N, X, Y <=100
1<= Ht[] <=1000

Example:
Input:
2
10 1 1
5
4 1 5
6 9 11 4 5

Output:
1
12

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

#include<iostream>
using namespace std;
int main()
{
int t ;
cin>>t ;
while(t--){
   int x,y,n ;
   cin>>x>>y>>n ;
 
   int arr[n] ;
   for(int i=0;i<n;i++)
       cin>>arr[i] ;
 
   int count=0 ;
   for(int i=0;i<n;i++){
       if(arr[i]<=x)
           count++ ;
       else{
           int temp=arr[i] ;
           while(temp>x){
               count++ ;
               temp=temp-(x-y) ;
           }
           count++ ;
       }
   }
   cout<<count<<endl ;
}
return 0;
}

---------------------------------------------------------------------------------

Comments

Post a Comment

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )