zoukankan      html  css  js  c++  java
  • 数字游戏(找规律)

    题目描述

    小易邀请你玩一个数字游戏,小易给你一系列的整数。你们俩使用这些整数玩游戏。每次小易会任意说一个数字出来,然后你需要从这一系列数字中选取一部分出来让它们的和等于小易所说的数字。 例如: 如果{2,1,2,7}是你有的一系列数,小易说的数字是11.你可以得到方案2+2+7 = 11.如果顽皮的小易想坑你,他说的数字是6,那么你没有办法拼凑出和为6 现在小易给你n个数,让你找出无法从n个数中选取部分求和的数字中的最小数。

    输入描述:

    输入第一行为数字个数n (n ≤ 20)
    第二行为n个数x
    i
    (1 ≤ x
    i
    ≤ 100000)

    输出描述:

    输出最小不能由n个数选取求和组成的数
    示例1

    输入

    3
    5 1 2

    输出

    4
     1 /**
     2  *  数字游戏
     3  *      1、排序
     4           2、 遍历求和 ,当现有的和小于下一个数时 就会有空缺
     5  * @author Dell
     6  *
     7  */
     8 import java.util.ArrayList;
     9 import java.util.List;
    10 import java.util.Scanner;
    11 
    12 public class Main {
    13     static public int n = 3;
    14     static public List<Integer> list = new ArrayList();
    15     static {
    16         list.add(5);
    17         list.add(1);
    18         list.add(2);
    19         list.sort(null);
    20     }
    21 
    22     static public int f() {
    23         list.sort(null);
    24         int sum = 0;
    25         for (Integer integer : list) {
    26             if (sum + 1 < integer) {
    27                 return sum + 1;
    28             } else {
    29                 sum += integer;
    30             }
    31         }
    32         return sum + 1;
    33     }
    34 
    35     public static void main(String[] args) {
    36         Scanner sc = new Scanner(System.in);
    37         n = Integer.parseInt(sc.nextLine());
    38         String[] str = sc.nextLine().split(" ");
    39         list = new ArrayList();
    40         for (int i = 0; i < n; i++) {
    41             list.add(Integer.parseInt(str[i]));
    42         }    
    43         System.out.println(f());    
    44     }
    45 }
    46     
  • 相关阅读:
    Java内部类总结
    利用mysqld_multi启动管理多实例
    L001-老男孩教育-Python13期VIP视频-19节-pbb
    L014-oldboy-mysql-dba-lesson14
    L012-oldboy-mysql-dba-lesson12
    L013-oldboy-mysql-dba-lesson13
    L010-oldboy-mysql-dba-lesson10
    L011-oldboy-mysql-dba-lesson11
    L009-oldboy-mysql-dba-lesson09
    L007-oldboy-mysql-dba-lesson07
  • 原文地址:https://www.cnblogs.com/the-wang/p/8979442.html
Copyright © 2011-2022 走看看