360java笔试编程第二题20160328

题目内容如下:




按照个人理解,写出了ac率为20%的程序如下:


import java.util.Scanner;

public class replaceASCII {
    public static void main(String[] args){
        int index=0;
        String replaceChar;
        StringBuilder strbu;
        Scanner s = new Scanner(System.in);
        System.out.println("请输入数据:");
        int n = s.nextInt();
        int m = s.nextInt();
        s.nextLine();
        String str=s.nextLine();
        if(str.length()!=n){
            System.out.println("输入不合法");
            return;
        }
        for(int line=0;line<m;line++){
            index = s.nextInt();
            replaceChar = s.next();
            if(replaceChar.length()!=1){
                System.out.println("输入不合法");
                return;
            }
            strbu = new StringBuilder(str);
            strbu.replace(index-1,index,replaceChar);
            str = strbu.toString();
            System.out.println(replaceFun(str));
        }

    }

    public static int replaceFun(String string){
        int replaceTime = 0;
        while(string.contains("..")){
            string = string.replaceFirst("\\.\\.",".");
            replaceTime++;
        }
        return replaceTime;
    }
}


也不知道是哪里没有考虑到,求各位指教~~

相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页