2019 autumn recruit program written questions expansion of beef

Subject description:

  Taurus to forage for food. Taurus encountered one day in order to display their magic n heap of forage, beef as long as he is currently experiencing a pile of hay with the same size, it will eat fodder, the expansion doubled the size of its own. Beginning beef size is A, then gives the size of the stack beef successively encountered n forage. Calculate the final size of the beef. 


Enter a description:


Input consists of two lines, the first line contains two integers n and A (1 ≤ n ≤ 200, 1 ≤ A ≤ 1,000,000,000)
The second row includes n integer representing the fodder heap size a_i beef successively encountered (1 ≤ a_i ≤ 1,000,000,000)


Output Description:


  Output an integer representing the size of the final beef.

Examples 1 Input:
  . 5 1
  2 1 2 1. 3

Output Example 1:
  4

Analysis of ideas:

  The title is only one condition , "beef as long as he is currently experiencing a pile of hay with the same size, it will eat fodder, the expansion doubled the size of its own " when it receives directly determine if the current size of the forage and beef same, doubling the size of the beef. Received, the size of the final output of beef on OK.

 

Java code is as follows:

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int a = sc.nextInt();
        int m = a;
        int temp;
        for (int i = 0; i < n; i++) {
            temp = sc.nextInt();
            if(temp == m){
                m=m*2;
            }
        }
        System.out.println(m);
    }
}

 

Guess you like

Origin www.cnblogs.com/l199616j/p/11619330.html