“666”是一種網絡用語,大概是表示某人很厲害、我們很佩服的意思。最近又衍生出另一個數字“9”,意思是“6翻了”,實在太厲害的意思。如果你以為這就是厲害的最高境界,那就錯啦 —— 目前的最高境界是數字“27”,因為這是 3 個 “9”!
本題就請你編寫程式,将那些過時的、隻會用一連串“6666……6”表達仰慕的句子,翻譯成最新的進階表達。
輸入格式:
輸入在一行中給出一句話,即一個非空字元串,由不超過 1000 個英文字母、數字和空格組成,以回車結束。
輸出格式:
從左到右掃描輸入的句子:如果句子中有超過 3 個連續的 6,則将這串連續的 6 替換成 9;但如果有超過 9 個連續的 6,則将這串連續的 6 替換成 27。其他内容不受影響,原樣輸出。
輸入樣例:
it is so 666 really 6666 what else can I say 6666666666
輸出樣例:
it is so 666 really 9 what else can I say 27
1.正規表達式
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
String str = scan.nextLine();
str = str.replaceAll("6{9}6+", "27");
str = str.replaceAll("6{3}6+", "9");
System.out.println(str);
}
2.一般方法
import java.util.*;
public class Test1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String str = input.nextLine();
int left=0;
for(int i=0;i<str.length();i++) {
int count = 0;
left = i;
while(str.charAt(i)=='6') {
i++;
count++;
if(i>str.length()-1) {
break;
}
}
if(count>3&&count<=9) {
str = str.replaceFirst(str.substring(left, i), "9");
i = left;
}else if(count>9) {
str = str.replaceFirst(str.substring(left, i), "27");
i = left;
}
}
System.out.println(str);
}
}