Number game problems (iterative Java) __ iterations

Source: Internet
Author: User

1. The original question of cattle net:

Topic Description 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 the game. Every time a small easy will say a number out, and then you need to select a part of this series 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 you have, the small easy to say number is 11. You can get the solution 2+2+7 = 11. If naughty little easy to pit you, he said the number is 6, then you have no way to piece out and for 6 now small easy to give you n number, Lets you figure out the smallest number of numbers that can't be picked from the n number. Enter a description:

Enter first behavior number n (n≤20)
second behavior N number XI (1≤xi≤100000)
Output Description:
Output minimum cannot be selected by the number of n
Example 1 input
3
5 1 2
Output
4

2, the topic Analysis:

2.1, has been a misunderstanding, is the procedures to find out the number of numbers in the sequence, can represent the minimum min, and then in the find: After this number can not be the number sequence through the combination of the first smallest number. But the title of the topic is: the Min min should start from 1, then increment, that is, look for a positive range, the smallest integer can not be expressed.

2.2, read everybody's comment, originally this topic originates from the code jam, this Google competition's a website,

Give the original title URL: http://code.google.com/codejam/contest/4244486/dashboard#s=p2

Original method: http://code.google.com/codejam/contest/4244486/dashboard#s=a&a=2


3, code implementation: has a

Package schooloffer17;
Import java.util.ArrayList;
Import java.util.Collections;

Import Java.util.Scanner;
 /** * Created by Caoxiaohong on 17/11/6 11:33.
        * < Digital Games ></Digital Games > * Small easy to invite you to play a number game ... * * public class Numbergame {public static void main (string[] args) {
        Scanner scanner=new Scanner (system.in);
            int n;//input number while (Scanner.hasnext ()) {n=scanner.nextint ();
            Scanner.nextline ();//Line arraylist<integer> numbers=new arraylist<integer> (); for (int i=0;i<n;i++) Numbers.add (Scanner.nextint ());
            1≤xi≤100000 Collections.sort (numbers);
            If the smallest number is larger than 1, then you do not have to do the following calculation, direct return 1 can be. Because 1 is the smallest number that cannot be expressed.
                if (numbers.get (0) >1) {System.out.println (1);
            Continue
             /** * Iterative Process: * 1.min means: Numbers subscript from 0 to I of the combination of numbers, can represent the number range: [1,min];
  * 2. After the new addition of the digital Numbers.get (i), to           * [1,min]u[tmp]u[1+tmp,min+tmp]=[1,min+tmp], then request: tmp<=min+1 can * 3. Example: * */int min=1;
            Min is initialized to the value of the first element, int tmp;
                for (int i=1;i<n;i++) {tmp=numbers.get (i);
                if (tmp>min+1) break;
                else{min = tmp;
        } System.out.println (min+1);
 }
    }
}



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.