https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2536
困難度 ★ 本題較為麻煩要利用StringTokenizer將字串中的英文和數字拆開
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = Integer.parseInt(scn.nextLine());
for (int c = 1; c <= n; c++) {
String str = scn.nextLine();
StringTokenizer st = new StringTokenizer(str, "0123456789");//使用StringTokenizer將數字拆開
StringTokenizer st2 = new StringTokenizer(str, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");//使用StringTokenizer將英文拆開
String arr[] = new String[st.countTokens()], ary[] = new String[st2.countTokens()];//arr儲存英文字串 ary儲存次數
int i = 0;
while (st.hasMoreTokens()) { //hasMoreTokens()讀出並存入陣列
arr[i] = String.valueOf(st.nextToken());
ary[i++] = String.valueOf(st2.nextToken());
}
System.out.printf("Case %d: ", c);
for (i = 0; i < arr.length; i++) {
for (int j = Integer.parseInt(ary[i]); j > 0; j--) {//印出該次數字母
System.out.print(arr[i]);
}
}
System.out.println();
}
}
/*
題目:Q11541: Decoding
作者:1010
時間:西元 2016 年 7 月 */
}