栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

第三题1111

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

第三题1111

import lombok.Data;

import java.util.*;

public class maintest {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int maxUsers = 0;
        int R = input.nextInt();
        int[] num = new int[R+1];
        int user = 0;
        int flag = 0;
        int flagX = 1;
        int trueMax = 0;
        for (int i = 0; i <= R; i++){
            num[i] = input.nextInt();
        }
        user = input.nextInt();

        for (int i = R; i>=0; i--){
            if (Math.pow(2,i) > user){
                maxUsers += num[i];
                num[i] = 0;
                flag = i;
            }else {
                break;
            }
        }

        onePace onePace = new onePace();
        onePace.setFlag(flag);
        onePace.setMaxUsers(maxUsers);
        onePace.setNum(num);
        onePace.setFlag(flag);

        while (flagX == 1){
            int oneUser = 0;
            onePace.setoneUser(oneUser);
            int add = add(onePace);
            if (add > trueMax){
                trueMax = add;
            }

            int max = 0;
            for (int i = 0; i < flag; i++){
                if (num[i] != 0){
                    max += num[i]*Math.pow(2,i);
                }
            }
            if (max > user){
                flagX = 1;
            }else {
                flagX = 0;
            }
        }

        System.out.println(trueMax);
    }

    public static int add(onePace onePace){
        for (int i = onePace.flag - 1; i > 0; i--){
            if (onePace.num[i] != 0){
                if (onePace.oneUser + Math.pow(2,i) <= onePace.oneUser){
                    onePace.num[i]--;
                    onePace.oneUser += Math.pow(2,i);
                    if (onePace.oneUser == onePace.user){
                        return addnum(onePace.maxUsers);
                    }else {
                        add(onePace);
                    }
                }else {
                    if (judgeBefore(onePace.num,i) == 0){
                        onePace.num[i]--;
                        return addnum(onePace.maxUsers);
                    }else {
                        add(onePace);
                    }
                }
            }
        }
        return onePace.maxUsers;
    }

    public static int judgeBefore(int[] num, int flag){
        for (int i = flag-1; i >= 0; i--){
            if (num[i] != 0){
                return i;
            }
        }
        return 0;
    }

    public static int addnum(int maxUsers){
        return maxUsers+1;
    }

    @Data
    public static class onePace{
        int[] num;
        int oneUser;
        int maxUsers;
        int flag;
        int user;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/713010.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号