JAVA-Huawei real questions-bonus points

need:

    The boss of the company has made a big business and wants to allocate some bonuses to each employee. He wants to use a game to determine how much money each person will get. According to the order of employees' job numbers, a number is randomly selected for each person. Arrange according to the order of job numbers. If the first number is greater than your own number, then the employee in front can get a bonus of "distance * number difference". If you don't encounter a number larger than your own, assign yourself a random number of bonuses.

For example, the random numbers in order of job numbers are: 2,10,3.

The number 10 of the second employee is greater than the number 2 of the first employee, so the first employee can get 1 * (10-2) = 8.

There is no employee with a higher number behind the second employee, so he gets the bonus of the random number he was assigned, which is 10.

The third employee is the last employee, and there is no employee with a larger number behind him, so the bonus he gets is 3.

Please help the boss calculate how much bonus each employee will receive in the end.
 

Input description:
     The first line n represents the number of employees (including the last boss)
     and the second line is the random number assigned to each employee 

Output description:
     The final number of bonuses distributed to each employee

Input:
        3 --> Number
        2 10 3 --> Random number
Output:
        8 10 3 --> Result

coding:

public class TakePrize {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入员工数量:");
        int len = sc.nextInt();

        System.out.print("随机生成员工号:");
        //调用方法1
        List<Integer> list = norepeat(len);
        System.out.println(list.toString());

        //调用方法2
        List<Integer> ll =show(list);
        System.out.println("员工分的奖金数:"+ll.toString());
    }

    /**
     * 开始遍历,并查找到第一个比自己大的数,那么就自己的奖金就是这个数减自己的数,如果没有,就自己的奖金就是本身随机数。
     * @param list
     * @return
     */
    private static List<Integer> show(List<Integer> list) {
        int flag = 0;
        List<Integer> lists = new ArrayList<>();
        //循环比较
        for (int i = 0; i < list.size(); i++) {
            for (int j = i+1 ; j < list.size(); j++) {
                //判断前一个数是否大于后面的数
                if (list.get(i) < list.get(j)) {
                    Integer money = (list.get(j) - list.get(i)) * (j - i);
                    lists.add(money);
                    flag = 1;
                    break;
                }
            }
            //如果没有大于后面值
            if (flag == 0) {
                lists.add(list.get(i));
            }
            flag = 0; //重置
        }
        return lists;
    }


    /**
     * 随机数字不重复,员工数量(包含老板)范围1 ~ 10000
     *
     * @param count
     * @return
     */
    public static List<Integer> norepeat(int count) {
        //随机对象
        Random random = new Random();
        //set集合对象
        Set<Integer> set = new HashSet<>();
        //循环
        while (true) {
            //随机数范围1 ~ 10000
//            int number = random.nextInt(10000) + 1;
            int number = random.nextInt(10) + 1;
            set.add(number);
            //判断是否满足员工数量
            if (set.size() >= count) {
                break;
            }
        }
        //返回集合对象
        return new ArrayList<>(set);
    }
}

Effect: 

 

 

Guess you like

Origin blog.csdn.net/hlx20080808/article/details/132827228