[Hackerrank] Halloween party

Source: Internet
Author: User
Change language:

Alex is attending a Halloween party with his girlfriend Silvia. at the party, Silvia spots a giant chocolate bar. if the chocolate can be served as only 1x1 sized pieces and Alex can cut the chocolate bar exactlyKTimes, what is the maximum number of Chocolate pieces Alex can cut and give Silvia?

Input Format
The first line contains an integer t, the number of test cases.TLines follow.
Each line contains an integerK

Output Format
TLines. Each line contains an integer that denotes the maximum number of pieces that can be obtained for each test case.

Constraints
1 <=T<= 10
2 <=K<= 107

Note
Chocolate needed to be served in size of 1x1 size pieces.
Alex can't relocate any of the pieces, nor can he place any piece on top of other.

Question: The question falls into the input range. The answer should be in the long type.

Code:

 1 import java.io.*; 2 import java.util.*; 3 import java.text.*; 4 import java.math.*; 5 import java.util.regex.*; 6  7 public class Solution { 8    static long Halloween_party(long k){ 9        if(k %2 ==0)10            return (k/2)*(k/2);11        return (k/2)*(k/2+1);12    }13 14  public static void main(String[] args) {15      Scanner in = new Scanner(System.in);16      int t = in.nextInt();17      for(int i = 0;i < t;i++){18          long k = in.nextInt();19          System.out.println(Halloween_party(k));20      }21 22      23    }24 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.