Wednesday, 23 October 2013

Chocolate Feast

PROBLEM

Little Bob loves chocolates and goes to the store with a $N bill with $C being the price of each chocolate. In addition, the store offers a discount: for every M wrappers he gives the store, he’ll get one chocolate for free. How many chocolates does Bob get to eat?

Input Format:
The first line contains the number of test cases T (<=1000).
Each of the next T lines contains three integers N, C and M


Output Format:


Print the total number of chocolates Bob eats.

Constraints:
2 <= N <= 100000
1 <= C <= N
2 <= M <= N


Sample input

3
10 2 5
12 4 4
6 2 2
Sample Output

6
3
5

Explanation 


In the first case, he can buy 5 chocolates with $10 and exchange the 5 wrappers to get one more chocolate thus making the total number of chocolates he can eat as 6

In the second case, he can buy 3 chocolates for $12. However, it takes 4 wrappers to get one more chocolate. He can’t avail the offer and hence the total number of chocolates remains 3.

In the third case, he can buy 3 chocolates for $6. Now he can give 2 of this 3 wrappers and get 1 chocolate. Again, he can use his 1 unused wrapper and 1 wrapper of new chocolate to get one more chocolate. Total is 5.

PROGRAM                                              OUTPUT
#include <iostream>
using namespace std;
int main() {
  int t,n,c,m,rc,nc,fc,i,*a,nvc;
  cin>>t;
  a=new int[t];
  for(i=0;i<t;i++)
  {
     n=c=m=0;
     cin>>n>>c>>m;
     fc=nc=rc=nvc=0;
     rc=nc=(n/c);
     while(rc>=m)
     {
	fc=(rc/m);
        nvc=nvc+fc;
        rc=(rc%m)+fc;
     }
     a[i]=nc+nvc;
  }
  for(i=0;i<t;i++)
  {
     cout<<a[i]<<endl;
  }
	 
  return 0;
}
3
10 2 5 12 4 4 6 2 2
6 3 5

Sunday, 29 September 2013

Prime Number

A prime number is a natural number greater than 1 that has no positive divisor other than 1 and itself.

A Composite number is a positive integer that has at least one positive integer other than one or itself. 



Check number entered is  Prime or Composite.


PROGRAM                                              OUTPUT
#include <iostream>
using namespace std;

int main (){
  int num,n,prime;
  cout<<"Enter a number";
  cin>>num;

  if(num <=1 ){
    cout<<"Not Prime or Composite";
  }
else {
    prime = 1;
    for(n=2;n<=num-1;n++){
      if(num%n == 0){
       prime = 0;
       }
    }
    if(prime ){
    cout<<"Prime Number "<<endl;
    }
     else{
       cout<<"Composite Number";
    } 
   }
return 0;
}
Case 1:

Enter a number: 23

Prime Number.

Case 2:

Enter a number: 33

Composite Number

Prime Number

A prime number is a natural number greater than 1 that has no positive divisor other than 1 and itself.


Check number entered is  prime number or not.


PROGRAM                                              OUTPUT
#include <iostream>
using namespace std;

int main (){
  int num,n,prime;
  cout<<"Enter a number";
  cin>>num;

  if(num <=1 ){
    cout<<"NOT a Prime Number";
  }
else {
    prime = 1;
    for(n=2;n<=num-1;n++){
      if(num%n == 0){
       prime = 0;
       }
    }
    if(prime ){
    cout<<"Prime Number "<<endl;
    }
     else{
       cout<<"NOT a Prime Number";
    } 
   }
return 0;
}
Case 1:

Enter a number: 23

Prime Number.

Case 2:

Enter a number: 33

NOT a Prime Number

Prime Number

A prime number is a natural number greater than 1 that has no positive divisor other than 1 and itself.


Print prime number upto a user given number. 


PROGRAM                                              OUTPUT
#include <iostream>
using namespace std;

int main (){
  int num,i,n;
  bool prime; 
  cout<<"Prime no. upto:";
  cin>>num;
  for(i=3;i<=num;i++){
    prime = true;
    for(n=2;n<=i-1;n++){
      if(i%n == 0){
       prime = false;
       }
    }
    if(prime){
    cout<<i<<" is prime "<<endl;
    } 
   }
return 0;
}
Prime no. upto: 25
3
5
7
11
13
17
19
23

Saturday, 28 September 2013

FIZZBUZZ 

 A program that prints (to STDOUT) the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.



PROGRAM                                              OUTPUT
#include <iostream>
using namespace std;
int main()
{
    for(int i=1;i<=100;i++){

        if(i%15 == 0)

            cout<<"FizzBuzz\n";

        else if(i%3 == 0)

            cout<<"Fizz\n";

        else if(i%5 == 0)

            cout<<"Buzz\n";

        else

            cout<<i<<"\n";
     }
    return 0;
}
}
1
2
Fizz
4
Buzz
Fizz
7
.
.
14
FizzBuzz
.
.
.

.
.
.
Fizz
100

Shortest code in perl

for(1..100){$x=!($_%5);$y=!($_%3);$_=($y?"Fizz":"").($x?"Buzz":"")if($x+$y);print;print "\n";}

HackerRank


Sample Challenge 


This is a simple challenge to get things started. Given a sorted array (ar) and a number (V), can you print the index location of V in the array?
{The next section describes the input format. You can often skip it if you are using included methods. }

Input Format
There will be three lines of input:
  • V - the value you are looking for.
  • n - the size of the array.
  • ar - n numbers that makes up the array.
Output Format
Output the index of V in the array.
{The next section describes the constraints and ranges of the input. Occasionally you want to check here to see how big the input could be. }
Constraints

1<=n<=1000
-1000 <=x <= 1000 , x ∈ ar
{This “sample” shows the first input case. It is often useful to skip to the sample to understand a challenge. }
Sample Input
4
6
1 4 5 7 9 12
Sample Output
1
Explanation
V = 4. The 4 is located in the 1th spot on the array, so the answer is 1.
PROGRAM                                              OUTPUT
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;


int main() {
    
    int n,*ar,V,a,c;
    cin>>V>>n;
    c=0;
    ar = new int[n];
    for(int i=0;i<n;i++)
    {
        cin>>ar[i];
        if(V==ar[i]&&c==0)
        {
            c=1;
            a=i;
        }
    }
    cout<<a;   
    return 0;
}
Input 

4
6
1 4 5 7 9 12
 Output

   1