2-3 java-jmu-m02-寻找包含密码的字符串
有题解
分数 25
全屏浏览题目
单位 集美大学
输入密码x与次数n。然后在若干行字符串中(以end为结尾)寻找包含指定密码(x)的字符串的。
一旦找到就输出该字符串所在行数及该行字符串 。 最后输出包含密码x的字符串行数。
在寻找过程中,如果找到n次包含密码x的字符串则直接跳出循环,否则一直查找直到碰到end为止。
输入格式:
输入密码x
输入次数n
输入若干字符串以end结束
输出格式:
该字符串所在行数及包含密码x的字符串
符合条件的字符串出现的次数
输入样例1:
king
3
The king is coming!
Where is he now?
He's in the kingdom.
Are you sure?
!!!kingnevertelllie!!!
king is the king
end
输出样例1:
1 The king is coming!
3 He's in the kingdom.
5 !!!kingnevertelllie!!!
3
输入样例2:
java
3
Do you like coffe?
Yes, i like java.
java is also my favorite
King don't love coffe.
king is the king
end
输出样例2:
2 Yes, i like java.
3 java is also my favorite
2
代码长度限制
16 KB
时间限制
400 ms
内存限制
import java.util.Objects;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
String key = s1;
int n = sc.nextInt();
int count = 0;
int num = 0;
String s2;
while (!Objects.equals(s1, "end") && n >= 1) {
s1 = sc.nextLine();
s2 = s1;
if (s2.contains(key)) {
System.out.println(count + " " + s2);
num++;
n--;
}
count++;
}
System.out.println(num);
}
}
发这一题的原因只是因为我看网上这题的答案都一样,我就是发个另一种写法,其他没什么,题目很简单