题解 | 表示数字

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            String str = sc.next();
            StringBuffer sb = new StringBuffer(str);
            int i = 1;
            for (i = 1; i < sb.length(); i++) {
                if ((Character.isDigit(str.charAt(i)) && !Character.isDigit(str.charAt(i - 1)))
                   ) {//本索引是数字,而前一位不是的话,插入*
                    sb.insert(i, "*");
                    str = sb.toString();//把最新的字符串更新给str
                    i++;//测试用例中有1**3,当索引位移到3的时候,会无数次走进这个条件,所以索引要跳到下一位。
                } else if ((Character.isDigit(str.charAt(i - 1)) &&
                            !Character.isDigit(str.charAt(i)))) {////本索引不是数字,而前一位是的话,插入*
                    sb.insert(i, "*");
                    str = sb.toString();
                }

            }
            if ((Character.isDigit(str.charAt(0)))) {
                sb.insert(0, "*");
                System.out.print(sb.toString());
                if ((i == sb.length() - 1 - 1) || (Character.isDigit(str.charAt(i - 1)))) {
                    System.out.print("*");
                }

            } else {
                System.out.print(sb.toString());
                if ((i == sb.length() - 1 - 2) || (Character.isDigit(str.charAt(i - 1)))) {
                    System.out.print("*");
                }


            }
        }
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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