广联达笔试01

t01种草

public class t01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int x = sc.nextInt();
        int[] arr = new int[n];
        for (int i=0;i<n;i++){
            arr[i] = sc.nextInt();
        }
        System.out.println(solve(arr,m,x));
    }

    public static int solve(int[] arr,int m,int x){
        PriorityQueue<Integer> pq = new PriorityQueue<>();
        int max = Integer.MIN_VALUE;
        for (int i=0;i<arr.length;i++){
            pq.add(arr[i]);
            if (arr[i]>max){
                max = arr[i];
            }
        }
        while (m>0){
            int cur = pq.poll();
            while (m>0&&cur+x<max){
                cur+=x;
                m--;
            }
            pq.add(cur);
        }
        return pq.peek();
    }
}```



t02机器人

```java
public class t02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int C = sc.nextInt();
        int[] value = new int[n];
        float[] cost = new float[n];
        for (int i=0;i<n;i++){
            cost[i] = sc.nextFloat();
            value[i] = sc.nextInt();
        }
        System.out.println(solve(C,value,cost));
    }

    public static int solve(int bag,int[] value,float[] cost){
        int[][] dp = new int[value.length+1][bag+1];
        for (int i=1;i<dp.length;i++){
            for (int j=1;j<dp[0].length;j++){
                if (j-cost[i-1]<0){
                    dp[i][j] = dp[i-1][j];
                }else {
                    dp[i][j] = Math.max(dp[i-1][j],dp[i-1][(int) (j-cost[i-1])]+value[i-1]);
                }
            }
        }
        return dp[value.length][bag];

    }
}```


t03

```java
public class t03 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[] list = new int[n];
        for (int i=0;i<n;i++){
            list[i] = sc.nextInt();
        }
        List<Integer> res = adjustArray(list);
        for (int i=0;i<res.size();i++){
            System.out.print(res.get(i)+" ");
        }

    }
    public static List<Integer> adjustArray(int[] nums) {
        //使用优先队列对数据进行调
        List<Integer> list = new ArrayList<Integer>();
        for (int m : nums) {
            list.add(m);
        }
        Iterator<Integer> iterator = list.iterator();
        PriorityQueue<Integer> queue = new PriorityQueue<Integer>();
        //入队列
        while (iterator.hasNext()) {
            queue.offer(iterator.next());
        }

        for (int n = 0; n < queue.size(); n++) {
            //取出最小值
            int min = queue.poll();
            //计算重复次数
            int count = 0;
            //定位重复元素的索引
            int fristIndex = 0;
            int secondIndex = 0;
            //验证最小值是否重复
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i) == min) {
                    count++;
                    //数据重复
                    if (count == 1){
                        fristIndex = i;
                    }
                    if (count == 2) {
                        //定位第二个重复数据
                        secondIndex = i;
                        break;
                    }
                }
            }
            //如果有重复数据,删除第一个重复数据,修改第二个重复数据,并且更新队列
            if (count == 2) {
                int cur = list.get(secondIndex)*2;
                list.set(secondIndex, cur);
                list.remove(fristIndex);
                //修改后,更新队列
                queue.poll();
                queue.add(cur);
            }
            //更新计数器
            count = 0;
            //更新n
            n = 0;
        }
        return list;
        }
    }```

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值