andy6804tw
7/18/2016 - 12:55 AM

## http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=30764 這題因為不知每筆的長度為何故先存入字串再做切割,然後再將切割後的字串以子字串subsring拆開字母和數字存入二維陣列 本題是用氣泡排序法,

http://e-tutor.itsa.org.tw/e-Tutor/mod/programming/view.php?id=30764

``````import java.util.*;

public class Main {

public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = Integer.parseInt(scn.nextLine());
while (n-- != 0) {
String str = scn.nextLine(), s[] = str.split(" "), arr[][] = new String[s.length][2];
for (int i = 0; i < s.length; i++) {
arr[i][0] = s[i].substring(0, 1);
arr[i][1] = s[i].substring(1);
}
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
char c1[] = arr[i][0].toCharArray();
char c2[] = arr[j][0].toCharArray();
if (c1[0] == c2[0]) {
int n1 = Integer.parseInt(arr[i][1]), n2 = Integer.parseInt(arr[j][1]);
if (n1 < n2) {
String temp = arr[i][0];
arr[i][0] = arr[j][0];
arr[j][0] = temp;
temp = arr[i][1];
arr[i][1] = arr[j][1];
arr[j][1] = temp;
}
} else if (c1[0] < c2[0]) {
String temp = arr[i][0];
arr[i][0] = arr[j][0];
arr[j][0] = temp;
temp = arr[i][1];
arr[i][1] = arr[j][1];
arr[j][1] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
if (i != 0)
System.out.print(" ");
System.out.print(arr[i][0] + arr[i][1]);
}
System.out.println();
}
}
/*
題目：ITSA Basic 題目13. 撲克牌大小
作者：1010
時間：西元 2016 年 7 月 */
}``````