个人博客
谷歌面试题。
没啥好说的,模拟题型,按部就班打表实现即可。
import java.util.Scanner;
import java.util.Arrays;
public class Main {
static String[] number = {"zero","one","two","three","four","five","six","seven","eight","nine"};
static String[] prefix = {"","","double","triple","quadruple","quintuple",
"sextuple","septuple","octuple","nonuple","decuple"};
static String[] ans = new String[100];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
sc.nextLine();
for(int t = 1; t<= T; t++) {
String pnum = sc.next();
String[] f = sc.next().split("-");
int offset = 0; //pnum中的偏移量
int n = 0; //最终Case读出的个数
for(int i = 0; i < f.length; i++) {
int ed = Integer.parseInt(f[i]) + offset;
for(int j = offset; j < ed; j ++) {
// [offset, ed)
int k = j + 1;
int temp = pnum.charAt(j) - '0';
while(k < ed && pnum.charAt(k) == pnum.charAt(j)) k++;
if(k - j == 1)
ans[n++] = number[temp];
else if(k - j > 10) {
Arrays.fill(ans,n,n+k-j,number[temp]);
n = n+k-j;
}
else {
ans[n++] = prefix[k-j];
ans[n++] = number[temp];
}
j = k - 1;
}
offset = ed;
}
//Case #x: y
System.out.print("Case #" + t +": ");
for(int i = 0; i < n; i++) {
System.out.print(ans[i] + " ");
}
System.out.println();
}
}
}