题目:
思路:这个题也是遍历一遍数组,当遇到特殊字符0时进行处理。需要注意的是以下几点:
(1)我自己写输入输出的时候要注意,都是用next()。然后再进行转换
(2)在取出字符0的时候,直接给相应位置的设置为数字0就可以了。在输出的时候判断以下不是0再输出就可以了
代码:
import java.io.*; import java.util.*; public class zcy1 { public static void main(String[]args){ Scanner sc=new Scanner(System.in); String k1=sc.nextLine(); String a=sc.nextLine(); int k=Integer.valueOf(k1); if(a==null||k<1){ System.out.println(-1); } else{ char[]a1=a.toCharArray(); int count=0; int start=-1; for(int i=0;i<a1.length;i++){ if(a1[i]=='0'){ count++; start=start==-1?i:start; } else{ if(count==k){ while(count--!=0){ a1[start++]=0; } count=0; start=-1; } } } if(count==k){ while(count--!=0) a1[start++]=0; } for(int i=0;i<a1.length;i++){ if(a1[i]!=0) System.out.print(a1[i]); } } } }