题解 | #表示数字#

表示数字

https://www.nowcoder.com/practice/637062df51674de8ba464e792d1a0ac6

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        char[] ch = str.toCharArray();
        String[] arrstr = new String[ch.length];
        String[] res = new String[ch.length];
        for (int i = 0; i < ch.length; i++) {
            arrstr[i] = Character.toString(ch[i]);
            res[i] = Character.toString(ch[i]);
        }
        if (arrstr[0].matches("[0-9]") && arrstr[1].matches("[0-9]")) {
            res[0] = "*" + arrstr[0];
        }
        if (arrstr[0].matches("[0-9]") && arrstr[1].matches("[^0-9]")) {
            res[0] = "*" + arrstr[0] + "*";
        }
        if (arrstr[ch.length - 1].matches("[0-9]") &&
                arrstr[ch.length - 2].matches("[0-9]")) {
            res[ch.length - 1] = arrstr[ch.length - 1] + "*";
        }
        if (arrstr[ch.length - 1].matches("[0-9]") &&
                arrstr[ch.length - 2].matches("[^0-9]")) {
            res[ch.length - 1] = "*" + arrstr[ch.length - 1] + "*";
        }
        for (int i = 1; i < ch.length - 1; i++) {
            if (arrstr[i].matches("[0-9]") && arrstr[i - 1].matches("[^0-9]") &&
                    arrstr[i + 1].matches("[^0-9]")) {
                res[i] = "*" + arrstr[i] + "*";
            }
            if (arrstr[i].matches("[0-9]") && arrstr[i - 1].matches("[^0-9]") &&
                    arrstr[i + 1].matches("[0-9]")) {
                res[i] = "*" + arrstr[i];
            }
            if (arrstr[i].matches("[0-9]") && arrstr[i - 1].matches("[0-9]") &&
                    arrstr[i + 1].matches("[^0-9]")) {
                res[i] = arrstr[i] + "*";
            }
        }

        for (String s : res) {
            System.out.print(s);
        }
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务