題目描述
-
手機號有11位,同時校園內的同學都知道有手機短號(資費很實惠),手機號短號都是6+手機號後5位,比如:13512345678的手機短號對應爲:645678。
-
如果多個手機號的後5位相同,則多個重複號碼中後續的短號爲000000提示管理人員進行處理。
-
要求根據手機號,輸出對應短號!
樣例輸入:
2
13512345678
13787654321
注意:2爲輸入個數
樣例輸出:
645678
654321
提示:
對應重複號碼的處理:
輸入:
3
13758887173
13858887173
12345678910
將輸出:
687173
000000
678910
代碼
import java.util.ArrayList;
import java.util.Scanner;
public class MyTest12 {
public static void main(String[] args) {
// 多行輸入
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
ArrayList<String> phone = new ArrayList<>();
while (true){
Scanner sc1 = new Scanner(System.in);
String phone_num = sc1.nextLine();
phone.add(phone_num);
if (phone.size()==num){
break;
}
}
// 判斷尾號後5位是否相同
for (int i = 1; i <phone.size() ; i++) {
String now = phone.get(i).substring(phone.get(i).length()-5);
// System.out.println(now);
for (int j = 0; j <i; j++) {
boolean flag=phone.get(j).substring(phone.get(j).length()-5).equals(now);
if (flag){
String qian=phone.get(i).substring(0,phone.get(i).length()-5);
StringBuffer stringBuffer = new StringBuffer(qian);
stringBuffer.append("00000");
phone.set(i,stringBuffer.toString());
// System.out.println(phone);
}
}
}
// 短號
for (int i = 0; i <phone.size(); i++) {
String phoneNum = phone.get(i);
String shortNum = shortNum(phoneNum);
System.out.println(shortNum);
}
}
private static String shortNum(String num){
String numShort = num.substring(num.length() - 5);
String shortNum;
if (numShort.equals("00000")){
shortNum="0".concat(numShort);
}else{
shortNum="6".concat(numShort);
}
// System.out.println(shortNum);
return shortNum;
}
}