L1-058 6翻了(Java)

“666”是一种网络用语,大概是表示某人很厉害、我们很佩服的意思。最近又衍生出另一个数字“9”,意思是“6翻了”,实在太厉害的意思。如果你以为这就是厉害的最高境界,那就错啦 —— 目前的最高境界是数字“27”,因为这是 3 个 “9”!

本题就请你编写程序,将那些过时的、只会用一连串“6666……6”表达仰慕的句子,翻译成最新的高级表达。

输入格式:

输入在一行中给出一句话,即一个非空字符串,由不超过 1000 个英文字母、数字和空格组成,以回车结束。

输出格式:

从左到右扫描输入的句子:如果句子中有超过 3 个连续的 6,则将这串连续的 6 替换成 9;但如果有超过 9 个连续的 6,则将这串连续的 6 替换成 27。其他内容不受影响,原样输出。

输入样例:

it is so 666 really 6666 what else can I say 6666666666

输出样例:

it is so 666 really 9 what else can I say 27

解题思路

这个问题属于字符串处理的范畴,特别是涉及到查找和替换特定模式的字符串。在这个具体问题中,我们需要识别连续的 '6' 字符串,并根据它们的长度来进行相应的替换。

解决这个问题的关键步骤包括:

  1. 遍历字符串并计数连续的 '6' 字符。
  2. 根据连续 '6' 的数量决定替换策略:
    • 超过 9 个连续的 '6' 替换为 "27"。
    • 超过 3 个但不超过 9 个连续的 '6' 替换为 "9"。
    • 3 个及以下的连续 '6' 保持不变。
  1. 生成最终的字符串。

特别是在这段代码当中,如果currentChar == '6'时,这时候是不进行append的,而是count++,让lastChar = currentChar。直到currentChar != '6'时才进else判断当中去看6的数量是不是超过3个或者9个,如果没有的话才进行一个append

for (int i = 0; i < input.length(); i++) {
    char currentChar = input.charAt(i);
    
    if (currentChar == '6') {
        count++;
    } else {
        if (lastChar == '6') {
            if (count > 9) {
                result.append("27");
            } else if (count > 3) {
                result.append("9");
            } else {
                for (int j = 0; j < count; j++) {
                    result.append('6');
                }
            }
        }
        result.append(currentChar);
        count = 0;
    }
    lastChar = currentChar;
    }

解题过程中遇到的问题

暂无

代码

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        String output = translateExpression(input);
        System.out.println(output);
    }

    public static String translateExpression(String input) {
        StringBuilder result = new StringBuilder();
        int count = 0; // 用于计数连续的 '6'
        char lastChar = '\0'; // 保存上一个字符

        for (int i = 0; i < input.length(); i++) {
            char currentChar = input.charAt(i);

            if (currentChar == '6') {
                count++;
            } else {
                if (lastChar == '6') {
                    if (count > 9) {
                        result.append("27");
                    } else if (count > 3) {
                        result.append("9");
                    } else {
                        for (int j = 0; j < count; j++) {
                            result.append('6');
                        }
                    }
                }
                result.append(currentChar);
                count = 0;
            }
            lastChar = currentChar;
        }

        // 处理字符串末尾的连续 '6'
        if (lastChar == '6') {
            if (count > 9) {
                result.append("27");
            } else if (count > 3) {
                result.append("9");
            } else {
                for (int j = 0; j < count; j++) {
                    result.append('6');
                }
            }
        }

        return result.toString();
    }
}

--------2024.1.21补充----------

当做了L1-059 敲笨钟和L1-064 估值一亿的AI核心代码 我就在想这三题都是同一种类型是字符串处理。都是很适合正则表达式来做!甚至更加的简洁!

这一种写法适合在不需要额外的逻辑处理或复杂匹配规则的场景中使用。

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = scanner.nextLine();
        String output = translateExpression(input);
        System.out.println(output);
    }

    private static String translateExpression(String input) {
        // 首先替换超过9个连续的6
        input = input.replaceAll("6{10,}", "27");
        // 然后替换4到9个连续的6
        return input.replaceAll("6{4,9}", "9");
    }
}

还有这种写法,如果应用场景涉及到频繁的、复杂的正则表达式匹配和替换,或者需要更多控制匹配过程的能力(比如分组、条件匹配等),那么使用 PatternMatcher 类会更合适

import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

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

        String input = scanner.nextLine();
        String output = translateExpression(input);
        System.out.println(output);
    }

    private static String translateExpression(String input) {
        Pattern pattern = Pattern.compile("6{4,}");
        Matcher matcher = pattern.matcher(input);
        StringBuffer sb = new StringBuffer();

        while (matcher.find()) {
            String match = matcher.group();
            if (match.length() > 9) {
                matcher.appendReplacement(sb, "27");
            } else {
                matcher.appendReplacement(sb, "9");
            }
        }
        matcher.appendTail(sb);

        return sb.toString();
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Micek

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值