Quantcast
Channel: CodeChef Discuss - latest questions
Viewing all articles
Browse latest Browse all 40121

Chef the Brave Warrior Editorial

$
0
0

Problem Link-https://www.codechef.com/problems/EXOCODE5

Author:https://www.codechef.com/users/vivek96

DIFFICULTY:Easy-Medium

PREREQUISITES-Big-Integer,Basic Java Programming

PROBLEM:Chef is a brave Warrior who with his group of young soldiers moves from one place to another to fight against his opponents. Before Fighting he just calculates two things,the difference between his soldier number and the type of opponent’s soldier number.

From this difference and the opponent army soldier number type (even or odd number)?, he decides whether to fight or not?

Print the difference and the type of opponent army soldier number!

Chef ‘s opponent number is never greater than Chef number.

Constraints:The numbers are less than 10^25

EXPLANATION:From the Question statement it is crystal clear that we have to find difference between chef army soldier number and opponent soldier number. Numbers are large so we have to use Big-Integer Class(The java.math.BigInteger class provides operations analogues to all of Java's primitive integer operators and for all relevant methods from java.lang.Math.)

Introduction to Big Integer-https://goo.gl/WZis9f

IN C/C++ -http://codeforces.com/blog/entry/16380

Using BigInteger class function subtract we calculated the difference,then we have to check whether opponent soldier number is even or odd. (A number is said to be even number if it leaves no remainder when divided by 2. There is an alternative definition of even number and it is as a number having any number from 0, 2, 4, 6 and 8 at its ones place is an even number. Examples of even numbers are 12, 66, 456, 9900 and 12342 etc. An odd number leaves a remainder when it is divided by 2. All those numbers having any one from 1, 3, 5, 7 and 9 at their ones places are also called odd numbers.)

So for Checking Whether number is even or odd we can use remainder function,so if number is even we have to print even else print odd

AUTHOR'S AND TESTER'S SOLUTIONS:

import java.util.Scanner;

import java.math.BigInteger;

class ChefBraveWarrior {

public static void main(String []arg)

{
    Scanner in = new Scanner(System.in);

    BigInteger a = new BigInteger(in.next());

    BigInteger b = new BigInteger(in.next());

    BigInteger diff = a.subtract(b);

    if(b.remainder(BigInteger.valueOf(2))==BigInteger.ZERO)
      {
        System.out.println(diff);
        System.out.println("even");
      }
    else
        {
        System.out.println(diff);
        System.out.println("odd");

    }
}

}

edit1-correction


Viewing all articles
Browse latest Browse all 40121

Trending Articles