CCCJava真题练习

目录

1.日志排序

3.太阳能板最大面积

4.分糖果

7.连续数组和

8.射击比赛游戏

11.停车场统计

12.数组去重排序

33.字符串比较

2.出错的或电路

5.计算面积


1.日志排序

package huawei;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;

/**
 * @Description 日志排序
 * @Author caocc
 * @Date 2022/11/2 22:47
 */
public class FirstCode01 {
    static class Info {
        String a;
        String b;
        String c;
        String d;

        public Info(String a, String b, String c, String d) {
            this.a = a;
            this.b = b;
            this.c = c;
            this.d = d;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.valueOf(sc.nextLine());

        PriorityQueue<Info> queue = new PriorityQueue<>((Comparator<Info>) (o1, o2) -> {
            if (Integer.parseInt(o1.a) - Integer.parseInt(o2.a) != 0){
                return  Integer.parseInt(o1.a) - Integer.parseInt(o2.a);
            }
            if (Integer.parseInt(o1.b) - Integer.parseInt(o2.b) != 0){
                return  Integer.parseInt(o1.b) - Integer.parseInt(o2.b);
            }

            if (Integer.parseInt(o1.c) - Integer.parseInt(o2.c) != 0){
                return  Integer.parseInt(o1.c) - Integer.parseInt(o2.c);
            }

            if (Integer.parseInt(o1.d) - Integer.parseInt(o2.d) != 0){
                return  Integer.parseInt(o1.d) - Integer.parseInt(o2.d);
            }

            return 0;
        });

        for (int i = 0; i < n; i++) {
            String[] strings = sc.nextLine().replace(".", ":").split(":");
            Info info = new Info(strings[0], strings[1], strings[2], strings[3]);
            queue.add(info);
        }

        while (!queue.isEmpty()){
            Info info = queue.poll();
            System.out.println(info.a + ":" + info.b + ":" + info.c + "." + info.d);
        }

    }
}

3.太阳能板最大面积

package huawei;

import java.util.Scanner;

/**
 * @Description 太阳能板最大面积
 * @Author caocc
 * @Date 2022/11/3 15:17
 */
public class FirstCode03 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String[] strings = sc.nextLine().split(",");
        int[] num = new int[strings.length];

        for (int i = 0; i < strings.length; i++) {
            num[i] = Integer.parseInt(strings[i]);
        }

        int L = 0;
        int R = num.length - 1;

        int max = 0;

        while (L < R) {
            max = Math.max(max, Math.min(num[L], num[R]) * (R - L));
            if (num[L] < num[R]) {
                L++;
            } else {
                R--;
            }
        }
        System.out.println(max);
    }
}

4.分糖果

package huawei;

import java.util.HashMap;
import java.util.Scanner;

/**
 * @Description 分糖果
 * @Author caocc
 * @Date 2022/11/3 15:23
 */
public class FirstCode04 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        HashMap<Integer, Integer> map = new HashMap<>();
        int num = sc.nextInt();
        System.out.println(process(num, map));

    }

    public static int process(int nums, HashMap<Integer, Integer> map) {
        if (map.containsKey(nums)){
            return map.get(nums);
        }
        if (nums == 1) {
            return 0;
        }
        int times = 0;
        if ((nums & 1) == 0) {
            times =  process(nums / 2, map) + 1;
        } else {
            int p1 = process((nums - 1) / 2, map) + 2;
            int p2 = process((nums + 1) / 2, map) + 2;
            times = Math.min(p1, p2);
        }

        map.put(nums, times);
        return times;
    }
}

7.连续数组和

package huawei;

import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;

/**
 * @Description 连续数组和
 * @Author caocc
 * @Date 2022/11/3 16:27
 */
public class FirstCode07 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int target = sc.nextInt();
        TreeMap<Integer, Integer> treeMap = new TreeMap<>();
        treeMap.put(0, 1);

        int[] nums = new int[n];
        for (int i = 0; i < n; i++) {
            nums[i] = sc.nextInt();
        }
        int sum = 0;
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            sum += nums[i];
            int preSum = sum - target;
            for (Map.Entry<Integer, Integer> entry : treeMap.entrySet()) {
                if (entry.getKey() <= preSum) {
                    count += entry.getValue();
                }else {
                    break;
                }
            }

            if (treeMap.containsKey(sum)) {
                treeMap.put(sum, treeMap.get(sum) + 1);
            } else {
                treeMap.put(sum, 1);
            }
        }
        System.out.println(count);
    }
}

8.射击比赛游戏

package huawei;

import java.util.*;

/**
 * @Description 统计射击比赛成绩
 * @Author caocc
 * @Date 2022/11/3 17:03
 */
public class FirstCode08 {

    static class Info {
        int id;
        int sum;

        public Info(int id, int sum) {
            this.id = id;
            this.sum = sum;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());
        String[] ids = sc.nextLine().split(",");
        String[] scores = sc.nextLine().split(",");


        HashMap<Integer, PriorityQueue<Integer>> map = new HashMap<>();


        for (int i = 0; i < ids.length; i++) {
            if (map.containsKey(Integer.parseInt(ids[i]))) {
                PriorityQueue<Integer> minStack = map.get(Integer.parseInt(ids[i]));
                if (minStack.size() == 3 && minStack.peek() < Integer.parseInt(scores[i])) {
                    minStack.poll();
                }
                minStack.add(Integer.parseInt(scores[i]));
            } else {
                PriorityQueue<Integer> minStack = new PriorityQueue<Integer>();
                minStack.add(Integer.parseInt(scores[i]));
                map.put(Integer.parseInt(ids[i]), minStack);
            }
        }

        PriorityQueue<Info> maxStack = new PriorityQueue<>(new Comparator<Info>() {
            @Override
            public int compare(Info o1, Info o2) {
                return o1.sum == o2.sum ? -(o1.id - o2.id) : -(o1.sum - o2.sum);
            }
        });

        for (Map.Entry<Integer, PriorityQueue<Integer>> entry : map.entrySet()) {
            Integer id = entry.getKey();
            PriorityQueue<Integer> items = entry.getValue();
            int sum = 0;
            for (Integer item : items) {
                sum += item;
            }
            maxStack.add(new Info(id, sum));
        }
        StringBuilder sb = new StringBuilder();
        while (!maxStack.isEmpty()) {
            Info info = maxStack.poll();
            sb.append(info.id).append(",");
        }

        sb.delete(sb.length() - 1, sb.length());

        System.out.println(sb.toString());
    }
}

11.停车场统计

package huawei;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @Description TODO
 * @Author caocc
 * @Date 2022/11/3 18:03
 */
public class FirstCode11 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] split = sc.nextLine().split(",");
        int[] nums = new int[split.length];

        for (int i = 0; i < split.length; i++) {
            nums[i] = Integer.parseInt(split[i]);
        }

        List<Integer> list = new ArrayList<>();
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] == 1) {
                count++;
            } else {
                if (count != 0) {
                    list.add(count);
                    count = 0;
                }
            }
        }
        if (count != 0) {
            list.add(count);
        }
        int cars = 0;

        for (Integer num : list) {
            if (num % 3 == 0) {
                cars += num / 3;
                continue;
            } else if (num % 2 == 0) {
                cars += num / 2;
                continue;
            }else if (num == 1){
                cars += 1;
                continue;
            }


            cars += num / 3;
            num = num % 3;
            cars += num / 2;
            num = num % 2;
            cars += num / 1;

        }
        System.out.println(cars);
    }
}

12.数组去重排序

package huawei;

import java.util.Comparator;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.Scanner;

/**
 * @Description TODO
 * @Author caocc
 * @Date 2022/11/3 18:38
 */
public class FirstCode12 {
    static class Info {
        int index;
        int times;
        int val;

        public Info(int index, int times, int val) {
            this.index = index;
            this.times = times;
            this.val = val;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        String[] str = sc.nextLine().split(",");
        int[] nums = new int[str.length];

        for (int i = 0; i < str.length; i++) {
            nums[i] = Integer.parseInt(str[i]);
        }

        HashMap<Integer, Info> map = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            if (map.containsKey(nums[i])) {
                Info info = map.get(nums[i]);
                info.times++;
            } else {
                Info info = new Info(i, 1, nums[i]);
                map.put(nums[i], info);
            }
        }

        PriorityQueue<Info> stack = new PriorityQueue<>(new Comparator<Info>() {
            @Override
            public int compare(Info o1, Info o2) {
                return o1.times == o2.times ? o1.index - o2.index : -(o1.times - o2.times);
            }
        });
        for (Info value : map.values()) {
            stack.add(value);
        }

        StringBuilder sb = new StringBuilder();

        while (!stack.isEmpty()){
            Info info = stack.poll();
            sb.append(info.val).append(",");
        }

        sb.delete(sb.length()-1,sb.length());
        System.out.println(sb);
    }
}

33.字符串比较

package huawei;

import java.util.Scanner;

/**
 * @Description TODO
 * @Author caocc
 * @Date 2022/11/4 14:33
 */
public class FirstCode33 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[] str1 = sc.nextLine().toCharArray();
        char[] str2 = sc.nextLine().toCharArray();
        int V = sc.nextInt();

        int L = 0;
        int R = 0;
        int sum = 0;
        int max = 0;
        while (R < str1.length){
            sum += Math.abs(str1[R] - str2[R]);

            if (sum <= V){
                max = Math.max(max, R - L + 1);
            }else {
                sum -= Math.abs(str1[L] - str2[L]);
                L++;
            }

            R++;
        }

        System.out.println(max);
    }
}

2.出错的或电路

package huawei;

import java.util.Scanner;

/**
 * @Description TODO
 * @Author caocc
 * @Date 2022/11/4 16:38
 */
public class SecondCode02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = Integer.parseInt(sc.nextLine());

        String str1 = sc.nextLine();
        String str2 = sc.nextLine();

        int oneCount = 0;
        int zeroCount = 0;

        for (int i = 0; i < n; i++) {
            if (str1.charAt(i) == '0') {
                zeroCount++;
            } else {
                oneCount++;
            }
        }

        int count = 0;

        for (int i = 0; i < n; i++) {
            if (str2.charAt(i) == '0') {
                if (str1.charAt(i) == '0') {
                    count += oneCount;
                } else {
                    count += zeroCount;
                }
            }
        }

        System.out.println(count);
    }
}

5.计算面积

package huawei;

import java.util.Scanner;

/**
 * @Description TODO
 * @Author caocc
 * @Date 2022/11/4 17:50
 */
public class SecondCode05 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] str = sc.nextLine().split(" ");
        int n = Integer.parseInt(str[0]);
        int end = Integer.parseInt(str[1]);

        str = sc.nextLine().split(" ");
        int x = Integer.parseInt(str[0]);
        int height = Integer.parseInt(str[1]);

        int sum = 0;

        for (int i = 1; i < n; i++){
            str = sc.nextLine().split(" ");
            int curX = Integer.parseInt(str[0]);
            int curHeight = Integer.parseInt(str[1]);
            sum += Math.abs((curX - x) * height);

            x = curX;
            height += curHeight;
        }

        sum += Math.abs((end - x) * height);


        System.out.println(sum);
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值