攀拓甲级真题练习 (1006 - 1010)

1006 Sign In and Sign Out

请添加图片描述

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        int M = scanner.nextInt();
        String firstId = null, lastId = null;
        String earliestSignIn = "24:00:00", latestSignOut = "00:00:00";
        
        for (int i = 0; i < M; i++) {
            String id = scanner.next();
            String signInTime = scanner.next();
            String signOutTime = scanner.next();
            
            if (signInTime.compareTo(earliestSignIn) < 0) {
                earliestSignIn = signInTime;
                firstId = id;
            }
            
            if (signOutTime.compareTo(latestSignOut) > 0) {
                latestSignOut = signOutTime;
                lastId = id;
            }
        }
        
        System.out.println(firstId + " " + lastId);
        scanner.close();
    }
}

1007 Maximum Subsequence Sum

请添加图片描述

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        int K = scanner.nextInt();
        int[] numbers = new int[K];
        
        for (int i = 0; i < K; i++) {
            numbers[i] = scanner.nextInt();
        }

        int maxSum = Integer.MIN_VALUE;
        int currentSum = 0;
        int start = 0, end = 0, tempStart = 0;

        for (int i = 0; i < K; i++) {
            currentSum += numbers[i];

            if (currentSum > maxSum) {
                maxSum = currentSum;
                start = tempStart;
                end = i;
            }

            if (currentSum < 0) {
                currentSum = 0;
                tempStart = i + 1;
            }
        }

        if (maxSum < 0) {
            System.out.println(0 + " " + numbers[0] + " " + numbers[K - 1]);
        }else{
            System.out.println(maxSum + " " + numbers[start] + " " + numbers[end]);
        }
    
    }
}

1008 Elevator

请添加图片描述

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        
        int N = scanner.nextInt();
        int[] floors = new int[N];
        for (int i = 0; i < N; i++) {
            floors[i] = scanner.nextInt();
        }

        int totalTime = 0;
        int currentFloor = 0;

        for (int i = 0; i < N; i++) {
            int targetFloor = floors[i];
            if (targetFloor > currentFloor) {
                totalTime += (targetFloor - currentFloor) * 6;
            } else if (targetFloor < currentFloor) {
                totalTime += (currentFloor - targetFloor) * 4;
            }
            totalTime += 5; 
            currentFloor = targetFloor;
        }

        System.out.println(totalTime);
        scanner.close();
    }
}

1009 Product of Polynomials

请添加图片描述

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

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

        int K1 = scanner.nextInt();
        Map<Integer, Double> poly1 = new TreeMap<>();
        for (int i = 0; i < K1; i++) {
            poly1.put(scanner.nextInt(), scanner.nextDouble());
        }

        int K2 = scanner.nextInt();
        Map<Integer, Double> poly2 = new TreeMap<>();
        for (int i = 0; i < K2; i++) {
            poly2.put(scanner.nextInt(), scanner.nextDouble());
        }

        Map<Integer, Double> result = new TreeMap<>((a, b) -> b - a);
        for (int e1 : poly1.keySet()) {
            for (int e2 : poly2.keySet()) {
                int exp = e1 + e2;
                double coeff = poly1.get(e1) * poly2.get(e2);
                result.put(exp, result.getOrDefault(exp, 0.0) + coeff);
            }
        }

        result.entrySet().removeIf(entry -> Math.abs(entry.getValue()) < 1e-9);

        StringBuilder output = new StringBuilder();
        output.append(result.size());
        for (Map.Entry<Integer, Double> entry : result.entrySet()) {
            output.append(" ").append(entry.getKey()).append(" ").append(String.format("%.1f", entry.getValue()));
        }

        System.out.println(output.toString());
        scanner.close();
    }
}

1010 Radix

请添加图片描述

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String N1 = scanner.next();
        String N2 = scanner.next();
        int tag = scanner.nextInt();
        int radix = scanner.nextInt();
        scanner.close();

        if (tag == 2) {
            String temp = N1;
            N1 = N2;
            N2 = temp;
        }

        long num1 = convertToDecimal(N1, radix);
        int minRadix = findMinRadix(N2);

        long resultRadix = findRadix(N2, num1, minRadix, Math.max(num1 + 1, minRadix + 1));
        if (resultRadix == -1) {
            System.out.println("Impossible");
        } else {
            System.out.println(resultRadix);
        }
    }

    private static long convertToDecimal(String number, int radix) {
        long result = 0;
        for (char c : number.toCharArray()) {
            result = result * radix + (Character.isDigit(c) ? c - '0' : c - 'a' + 10);
            if (result < 0) {
                return -1; // overflow
            }
        }
        return result;
    }

    private static int findMinRadix(String number) {
        int minRadix = 2;
        for (char c : number.toCharArray()) {
            int value = Character.isDigit(c) ? c - '0' : c - 'a' + 10;
            if (value + 1 > minRadix) {
                minRadix = value + 1;
            }
        }
        return minRadix;
    }

    private static long findRadix(String number, long target, long low, long high) {
        while (low <= high) {
            long mid = (low + high) / 2;
            long converted = convertToDecimal(number, (int) mid);
            if (converted == -1 || converted > target) {
                high = mid - 1;
            } else if (converted < target) {
                low = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }
}

1007 最大子序列最后一个示例是超时的
代码本身时间复杂度O(n)
在leetcode,相同做法2ms
在攀拓200ms,示例也是不让看的,希望后台输入数据的老师手速再快点^^

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值