算法小白题

CCPC-Wannafly Winter Camp Day2 (Div2,A题)

题目描述

给定n个正整数,a1,a2,a3,…,an,使他们组成了序列A。
你的任务是删除其中 (n−2) 数字,使得 A 中剩余的数字连起来的所表示的数字最大,并给出这个值。

  • 严格来讲,如果剩余数字在 A 中的下标为p1,p2,p3,…pn。(1 <= p1 < p2 < …< pm <= n),则它们连起来所表示的数字为a1a2…an
输入描述

输入包含多组测试数据。

  • 第一行包含一个整数 T,表示测试数据的组数。随后的内容是各组测试数据。对于每组测试数据:
  • 第一行包含一个整数 n
  • 第二行包含 n 个整数,a1,a2,…an,保证每个数字不含前导零。
  • 1 <= T <= 3000
  • 2 <= n <= 6000
  • 1 <= ai <= 109
  • 所有测试数据的n之和不超过6000
输出描述

对于每组测试数据,输出一行Case #x: y(不含引号),其中x是测试数据的编号(从 1 开始编号),y是这组数据的答案。

样例输入

3
3
6 6 6
4
21 12 12 21
2
998244353 985661441

样例输出

Case #1: 66
Case #2: 2121
Case #3: 998244353985661441

分析
  • 根据题意得需要在给出的n个整数中找两个数组合成最大的数
  • 但是因为是把数字拼接起来,所以不能单纯找最大的两个组合起来
    • 例如:104 700 100
    • 显然:104700组合小于700100组合
  • 而且不能简单通过比较最大数前后数来判断组合后的大小
    • 例如:104 700 110
    • 显然:第一个例子是最大数前面大于后面,这个是后面大于前面,但组合结果一致
    • 这个答案应为 700110
  • 所以最终我的做法是:先找到最大数,再找到最大数左边的次大数和最大数右边的次大数。
  • 再比较二者的大小,选择最大的后输出。
代码
  • ps:根据数据范围的大小,组合后的数有两种存储比较方法
    1.使用import java.math.BigInteger;导入大整形,并存储组合后的数据。
    2.使用字符串来存储组合后的数据,当组合的字符串长度相等时,返回最长的字符串。否则使用字符串的比较,即compareTo()函数。
  • ps2:因为要找到最大数即最大数左右的次大数,所以存在两种方式:
    1.找到最大数,再找最大数左次大数,再找最大数右次大数。(时间复杂度为O(n + n / 2 + n / 2))
    2.循环替换来一趟找到最大数即左右的次大数(时间复杂度为O(n))
    • 使用两个其他变量,一个记录左边最大数(preMax),一个记录右边最大数(afMax)。当找到比当前最大数(max)还大的数(newNum)时,preMax = max,max = newNum。如果newNum < max。则和afMax比较。
  • endPs:作者是小白,如果有错误,欢迎指正。

方法一
import java.util.Scanner;

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

        //总组数
        int sumNum = in.nextInt();
        String []resArr = new String[sumNum];
        for (int i = 0; i < sumNum; i++) {
            //每组包含的数字数
            int evNum = in.nextInt();
            //存储输入的数字
            int []arr = new int[evNum];
            //存储当前最大值.最大值左边最大值,最大值右边最大值的序号
            int max = 0;
            int preMax = 0;
            int afMax = 0;
            //记录最大值出现与否
            boolean up = false;
            for (int j = 0; j < evNum; j++) {
                int num = in.nextInt();
                if (j == 0){
                    max = num;
                } else {
                    if (num > max) {
                        preMax = max;
                        max = num;
                        afMax = 0;
                        up = false;
                    } else if (num == max) {
                        if (!up) {
                            up = true;
                        }
                    } else {
                        if (num > afMax) {
                            afMax = num;
                        }
                    }
                }
            }
            if (up){
                resArr[i] = "" + max + max;
            } else {
                String a = "" + preMax + max;
                String b = "" + max + afMax;
                //如果二者长度相同, 使用字符串比较函数, 否则返回长度长的字符串
                if (a.length() == b.length()) {
                    resArr[i] = a.compareTo(b) > 0? a: b;
                } else if (a.length() > b.length()) {
                    resArr[i] = a;
                } else {
                    resArr[i] = b;
                }
            }
        }
        for (int i = 0; i < sumNum; i++) {
            if (i != 0) {
                System.out.println();
            }
            System.out.printf("Case #%d: %s", (i + 1), resArr[i]);
        }
    }
}
方法二
import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int T = input.nextInt();
        int count = 0;
        BigInteger[] result = new BigInteger[T];
        while(T -- > 0){
            int n = input.nextInt();
            int[] nums  = new int[n];
            for (int i = 0; i < n; i++) {
                nums[i] = input.nextInt();
            }
            int maxIndex = 0;
            int max = 0;
            for (int i = 1; i < n-1; i++) {
                if (max < nums[i]){
                    max = nums[i];
                    maxIndex = i;
                }
            }
            int secN = -1;
            for (int i = maxIndex+1; i < n; i++) {
                if (secN < nums[i]){
                    secN = nums[i];
                }
            }
            int secP = nums[0];
            for (int i = 0; i < maxIndex; i++) {
                if (secP < nums[i]){
                    secP = nums[i];
                }
            }
            BigInteger a = new BigInteger(Integer.toString(secP)+Integer.toString(max));
            BigInteger b = new BigInteger(Integer.toString(max)+Integer.toString(secN));
            BigInteger c = new BigInteger(Integer.toString(secP)+Integer.toString(secN));

            result[count++] = n == 2 ? new BigInteger(Integer.toString(nums[0])+Integer.toString(nums[1])):a.max(b).max(c);

        }
        for (int i = 0; i < result.length; i++) {
            if (i == result.length-1){
                System.out.print("Case #"+(i+1)+": "+result[i]);
            }
            else
                System.out.println("Case #"+(i+1)+": "+result[i]);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值