题目:
输入一个字典,用*****结尾,然后再在一行中输入若干单词,回车结束。
每个单词w,都需要在字典中找出所有可以用w的字母重排后得到的单词,
并按照字典序从小到大的顺序在一行中输出。
如果不存在,输出:( 。
输入单词之间用空格或空行隔开。
注意,字典中的单词不一定按字典排列。
例子:
输入:tarp given score refund only trap work earn course pepper part
******
resco nfudre aptr sett oresuc
输出:score
refund
tarp trap part
:(
course
解:
因为我是做完才看到从小到大的,我也懒得改了-_-!,所以我是按字典的输入顺序输出的。
public class Main {
public static String fun(String s) {
char[] c = s.toCharArray();
Arrays.sort(c);
return String.valueOf(c);
}
public static void main(String[] args) throws IOException {
Scanner out = new Scanner(System.in);
String[][] a = new String[2][50];
int i = 0;
do {
String s = out.next();
if (s.equals("******"))
break;
a[0][i] = s;
a[1][i] = fun(s);
i++;
} while (true);
out.nextLine();
String s = out.nextLine();
String[] b = s.split(" ");
for (String ss : b) {
ss = fun(ss);
boolean is = false;
for (int c = 0; c < a[0].length; c++) {
if (ss.equals(a[1][c])) {
is = true;
System.out.print(a[0][c] + " ");
}
}
if (!is) {
System.out.println(":(");
} else {
System.out.println();
}
}
}
}
387

被折叠的 条评论
为什么被折叠?



