样例输入
4
School boy student tree
ybo
样例输出
boy
Java单词恢复~求高手解答下。输入字符串的个数~然后输入一个乱了顺序的字符串,~
答案:2 悬赏:70
解决时间 2021-11-29 18:54
- 提问者网友:乏味沐染
- 2021-11-28 21:31
最佳答案
- 二级知识专家网友:万千宠爱
- 2021-11-28 21:37
替你写了段代码,测试通过。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int lineCount = 0;
int length = 0;
String[] words = null;
Scanner cin = new Scanner(System.in);
System.out.print("");
while (cin.hasNext()){
lineCount++;
String str = cin.nextLine();
String[] substr = str.split(" ");
switch (lineCount) {
case 1 :
length = Integer.parseInt(substr[0]);
words = new String[length];
break;
case 2 :
for (int i = 0; i < length; i++) {
words[i] = substr[i];
}
break;
case 3 :
str = substr[0];
for (int i = 0; i < length; i++) {
String word = words[i];
if (word.length() == str.length()) {
boolean sameFlag = true;
int j = 0;
while (j < str.length() && sameFlag) {
String ch = str.substring(j, j + 1);
sameFlag = word.contains(ch);
j++;
}
if (sameFlag) {
System.out.println(word);
}
}
}
break;
}
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int lineCount = 0;
int length = 0;
String[] words = null;
Scanner cin = new Scanner(System.in);
System.out.print("");
while (cin.hasNext()){
lineCount++;
String str = cin.nextLine();
String[] substr = str.split(" ");
switch (lineCount) {
case 1 :
length = Integer.parseInt(substr[0]);
words = new String[length];
break;
case 2 :
for (int i = 0; i < length; i++) {
words[i] = substr[i];
}
break;
case 3 :
str = substr[0];
for (int i = 0; i < length; i++) {
String word = words[i];
if (word.length() == str.length()) {
boolean sameFlag = true;
int j = 0;
while (j < str.length() && sameFlag) {
String ch = str.substring(j, j + 1);
sameFlag = word.contains(ch);
j++;
}
if (sameFlag) {
System.out.println(word);
}
}
}
break;
}
}
}
}
全部回答
- 1楼网友:蜜罐小熊
- 2021-11-28 21:46
理论上讲,很简单,但是实际写出来还是有点小恶心,代码如下,测试无误:
public class teststring {
public static void main(string[] args) {
docount("abc a bc abc bc abc", " ");
}
public static void docount(string sourcestring, string delimiter) {//sourcestring为需要统计的字符串,delimiter为字符串拆分符
list result = new arraylist();//用来进行统计操作的集合
list last = new arraylist();//存储统计结果的集合
string target[] = sourcestring.split(delimiter);//目标字符串数组
for (int i = 0; i < target.length; i++) {
result.add(target[i]);
}
do {
int num = 1;
string info = result.get(0);
result.remove(0);
for (int k = 0; k < result.size(); k++) {
if (info.equals(result.get(k))) {
num++;
result.remove(k);
}
}
last.add(num + "-" + info);
} while (result.size() > 0);
collections.sort(last);
collections.reverse(last);
iterator it = last.iterator();
while (it.hasnext()) {
string outcount[] = it.next().split("-");
system.out.println(outcount[1] + "的个数:" + outcount[0]);
}
}
}
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯