Monday, November 28, 2016

Army Game

Luke is daydreaming in Math class. He has a sheet of graph paper with  rows and  columns, and he imagines that there is an army base in each cell for a total of  bases. He wants to drop supplies at strategic points on the sheet, marking each drop point with a red dot. If a base contains at least one package inside or on top of its border fence, then it's considered to be supplied. For example:
image
Given  and , what's the minimum number of packages that Luke must drop to supply all of his bases?
Input Format
Two space-separated integers describing the respective values of  and .
Constraints
Output Format
Print a single integer denoting the minimum number of supply packages Luke must drop.
Sample Input 0
2 2
Sample Output 0
1
Explanation 0
Luke has four bases in a  grid. If he drops a single package where the walls of all four bases intersect, then those four cells can access the package:
image
Because he managed to supply all four bases with a single supply drop, we print  as our answer.

NOTE : WRITE YOUR PROGRAM IN COMMENTS SECTION

*******************
PROGRAM
******************

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int n,p; 
    int m; 
    scanf("%d %d",&n,&m);
    /* n is even and m is even */
    if(n%2==0 && m%2==0)
        printf("%d",(n*m)/4);
    /* n is even and m is odd */
    if(n%2==0 && m%2==1)
        {
        p=n*(m-1);
        p=p/4;
        p=p+(n/2);
        printf("%d",p);
    }
    /* n is odd and m is even */
    if(n%2==1&& m%2==0)
        {
        p=(n-1)*m;
        p=p/4;
        p=p+(m/2);
        printf("%d",p);
    }
    /* n is odd and m is odd */
    if(n%2==1&& m%2==1)
        {
        p=(n-1)*(m-1);
        p=p/4;
        p=p+((n+m)/2);
        printf("%d",p);
    }
   
    return 0;
}

Contact Form

Name

Email *

Message *