将一个字符串中所有的整数前后加上符号“*”,其他字符保持不变。连续的数字视为一个整数。
数据范围:字符串长度满足
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (Character.isDigit(c)) { if (i == 0) { sb.append("*"); } // 当前是数字上一个字符不是数字 if (i - 1 >= 0 && !Character.isDigit(str.charAt(i - 1))) { sb.append("*"); } sb.append(c); // 当前字符是数字并且下一个不是数字 if (i + 1 < str.length() && !Character.isDigit(str.charAt(i + 1))) { sb.append("*"); } if (i == str.length() - 1) { sb.append("*"); } } else { sb.append(c); } } System.out.println(sb); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String str = in.nextLine(); StringBuilder sb = new StringBuilder(); for(int i = 0;i < str.length();i++){ if(str.charAt(i) >= '0' && str.charAt(i)<='9'){ int j = i; sb.append("*"); while(j < str.length() && str.charAt(j) >= '0' && str.charAt(j)<='9'){ sb.append(str.charAt(j++)); } sb.append("*"); i = j - 1; }else{ sb.append(str.charAt(i)); } } System.out.print(sb.toString()); } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.nextLine(); StringBuilder sb = new StringBuilder(); boolean flag = false; for (int i = 0; i < s.length(); i++) { if (Character.isDigit(s.charAt(i))) { if (flag == false) { sb.append("*"); flag = true; } sb.append(s.charAt(i)); if (i == s.length() - 1) sb.append("*"); } else { if (flag) { sb.append("*" + s.charAt(i)); flag = false; } else { sb.append(s.charAt(i)); } } } System.out.println(sb.toString()); } }
import java.util.ArrayList; import java.util.List; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str = in.nextLine(); StringBuilder sb = new StringBuilder(str); for(int i = 0; i<sb.length(); i++) { if(sb.charAt(i) >= '0' && sb.charAt(i) <= '9') { if(i == 0 || sb.charAt(i-1)<'0' || sb.charAt(i-1)>'9') { //System.out.print(i + ": " +sb.charAt(i) +"\n"); sb.insert(i,'*'); i++; } if(i== sb.length()-1||sb.charAt(i+1) <'0' || sb.charAt(i+1)>'9') { sb.insert(i+1, '*'); i++; } } } System.out.print(sb); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 String a = in.nextLine(); char[] b = a.toCharArray(); StringBuffer sb = new StringBuffer(); for(int i=0;i<b.length;i++){ char c = b[i]; if(c>='0' && c <='9'){ if(i==0 || (b[i-1] <'0' || b[i-1] >'9')){ sb.append("*"); } sb.append(c); if(i==b.length-1 || (b[i+1] <'0' || b[i+1] >'9')){ sb.append("*"); } }else{ sb.append(c); } } System.out.print(sb.toString()); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String source = in.nextLine(); boolean open = true; StringBuilder result = new StringBuilder(); for (int i = 0; i < source.length(); i++) { char c = source.charAt(i); boolean b = c >= '0' && c <= '9'; if (b && open) { result.append("*"); open = false; } else if (!b && !open) { result.append("*"); open = true; } result.append(c); if (i == source.length() - 1 && b) { result.append("*"); } } System.out.println(result); } }
import java.io.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; while ((str = br.readLine()) != null) { int len = str.length(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < len - 1; i++) { // 首个字符为数字 if (i==0 && ('0' <= str.charAt(i) && str.charAt(i) <= '9')) sb.append('*'); char ch1 = str.charAt(i); char ch2 = str.charAt(i + 1); // (ch1 < '0' || ch1 > '9') && ('0' <= ch2 && ch2 <= '9')表示符号后是数字 // ('0' <= ch1 && ch1 <= '9') && (ch2 < '0' || ch2 > '9')表示数字后是符号 if (((ch1 < '0' || ch1 > '9') && ('0' <= ch2 && ch2 <= '9')) || (('0' <= ch1 && ch1 <= '9') && (ch2 < '0' || ch2 > '9'))) { sb.append(ch1).append('*'); // 连续的数字或连续的符号 } else sb.append(ch1); } // 对最后一个字符进行判断,因为前面只拼接到倒数第二个字符,最后一个未拼接 char ch3 = str.charAt(len - 1); // 是数字则拼接数字再拼接* if ('0' <= ch3 && ch3 <= '9') sb.append(ch3).append('*'); // 是符号则直接拼接符号 else sb.append(ch3); System.out.println(sb.toString()); } } }
import java.util.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String str_old = in.nextLine(); char[] char_old = str_old.toCharArray(); StringBuilder str_new = new StringBuilder(); for (int i = 0; i < char_old.length; i++) { if (Character.isDigit(char_old[i]) && (i == 0 || !Character.isDigit(char_old[i - 1]))) { str_new.append("*"); str_new.append(char_old[i]); } else if (i > 0 && Character.isDigit(char_old[i - 1]) && !Character.isDigit(char_old[i])) { str_new.append("*"); str_new.append(char_old[i]); } else { str_new.append(char_old[i]); } if (Character.isDigit(char_old[i]) && i == char_old.length - 1) { str_new.append("*"); } } System.out.println(str_new); } }
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s1 = in.nextLine(); in.close(); StringBuilder sb = new StringBuilder(); int i = 0; while (i < s1.length()) { while (i < s1.length() && !Character.isDigit(s1.charAt(i))) { sb.append(s1.charAt(i)); i++; } if (i == s1.length()) { break; } int j = i; while (j < s1.length() && Character.isDigit(s1.charAt(j))) { j++; } sb.append("*").append(s1.substring(i, j)).append("*"); i = j; } System.out.println(sb.toString()); } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < str.length(); i++) { // 字符直接跳过 if(Character.isDigit(str.charAt(i))){ int start = i,end = i; while(end <str.length() && Character.isDigit(str.charAt(end))){ end++; } // 数字记录下来位置并替换 stringBuilder.append("*"+str.substring(start,end)+"*"); i = end - 1; }else { stringBuilder.append(str.charAt(i)); } } System.out.println(stringBuilder.toString()); } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; while((str=br.readLine())!=null){ StringBuffer sb=new StringBuffer(); for(int i=0;i<str.length();i++){ if(Character.isDigit(str.charAt(i))&&(i==0||!Character.isDigit(str.charAt(i-1)))){ sb.append("*"); } sb.append(str.charAt(i)); if(i==str.length()-1&&Character.isDigit(str.charAt(i))){ sb.append("*"); break; } if(i==str.length()-1){ break; } if(!Character.isDigit(str.charAt(i+1))&&Character.isDigit(str.charAt(i))){ sb.append("*"); } } System.out.println(sb.toString()); } } }