2017 NetEase Internet Push pen question-number game

Source: Internet
Author: User
Tags arrays sort

The original question is as follows:
Small easy to invite you to play a number game, small easy to give you a series of integers. You two use these integers to play games. Each time a small easy to say a number out, and then you need to choose from this series of numbers to make their sum equal to the number of small easy to say. For example: if {2,1,2,7} is a series of numbers that you have, the small easy-to-say number is 11. You can get the scheme 2+2+7 = 11. If naughty little Yi wants to pit you, he says the number is 6, then you have no way to piece together and for 6 now small easy to give you n number, Lets you find the smallest number of numbers that cannot be summed from the N number of selected parts.
Input Description:

    Input first behavior number n (n≤20)
    second behavior N number XI (1≤xi≤100000)

Output Description:

    The minimum number of outputs cannot be selected by the sum of n numbers

Input Example:

   3
   5 1 2

Output Example:

   4

Problem-solving ideas: To find the minimum number can not be obtained, then must be two number of the sum is smaller. That is, the sum of the two numbers does not have the number k, and K is not in the provided list, then this number is the number to find. The resulting set of numbers can be taken any number of two, sum, the resulting storage sort stored in the array, and then it is compared with the ordered array of 1-n, can not match the first number is to find the most decimal.

Mistake: According to the above thinking, how to solve the unexpected. Trapped.
To see the answer: there is a better idea. The resulting input array is sorted first so that an ordered array is obtained. Then loop, the maximum number of cycles is the array size n. The first cycle increments the base number temp by a[i], and the base number temp is 0 before the loop. When the first i+1 cycle, the value of a[i+1] is greater than temp+1, it indicates that this number is not at this time (temp+1). Stop the loop and output temp+1.
The reason for doing so is where.

Referencing block content

The code is as follows:

import java.util.*;
        public class Numgame {public static void main (string[] args) {Scanner in = new Scanner (system.in);
            while (In.hasnext ()) {int n = in.nextint ();
            Int[] A = new int[n];
            for (int i = 0; i < n; i++) {A[i] = In.nextint ();
            }//Call Arrays's Sort method to sort the array directly arrays.sort (a);
            int base = 0; for (int i = 0; i < n; i++) {if (a[i]>base+1) {bre
                Ak
            } Base=base+a[i];
        } System.out.println (base+1); }

    }

}
Related Article

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.