《算法笔记》3.2小节——入门模拟->查找元素

问题 A: 统计同成绩学生人数

题目

题目描述

读入 N 名学生的成绩,将获得某一给定分数的学生人数输出。

输入

测试输入包含若干测试用例,每个测试用例的格式为

第 1 行:N 第 2 行:N 名学生的成绩,相邻两数字用一个空格间隔。 第 3 行:给定分数

当读到 N=0 时输入结束。其中 N 不超过 1000,成绩分数为(包含)0 到 100 之间的一个整数。

输出

对每个测试用例,将获得给定分数的学生人数输出。

样例输入

1
2
3
4
5
6
7
8
9
10
4
70 80 90 100
80
3
65 75 85
55
5
60 90 90 90 85
90
0

样例输出

1
2
3
1
0
3

题解

思路

这一题的思路还是很清晰的,无非就是循环中需要读取下一个 n,别遗漏了就好。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
while (n != 0) {
int score[] = new int[n + 1];
for (int i = 0; i < n; i++) {
score[i] = scanner.nextInt();
}
int tar = scanner.nextInt(), temp = 0;
int i = 0;
for (; i < n; i++) {
if (tar == score[i]) {
temp++;
}
}
System.out.println(temp);
n = scanner.nextInt();
}
}
}

问题 B: 找x

题目

题目描述

输入一个数 n,然后输入 n 个数值各不相同,再输入一个值 x,输出这个值在这个数组中的下标(从 0 开始,若不在数组中则输出 -1 )。

输入

测试数据有多组,输入 n ( 1 <= n <= 200 ),接着输入 n 个数,然后输入 x。

输出

对于每组输入,请输出结果。

样例输入

1
2
3
4
1 2 3 4
3

样例输出

1
2

题解

思路

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

while (sc.hasNext()) {
// 输入
int n = sc.nextInt();
int num[] = new int[n + 1];
for (int i = 0; i < n; i++) {
num[i] = sc.nextInt();
}
int tar = sc.nextInt();
// 查找
int i = 0;
for (; i < n; i++) {
if (num[i] == tar) {
System.out.println(i);
break;
}
}
if (i >= n) {
System.out.println("-1");
}
}
}
}

问题 C: 查找学生信息

题目

题目描述

输入 N 个学生的信息,然后进行查询。

输入

输入的第一行为 N,即学生的个数( N <= 1000 )

接下来的 N 行包括 N 个学生的信息,信息格式如下:

01 李江 男 21

02 刘唐 男 23

03 张军 男 19

04 王娜 女 19

然后输入一个 M ( M <= 10000 ),接下来会有 M 行,代表 M 次查询,每行输入一个学号,格式如下:

02

03

01

04

输出

输出 M 行,每行包括一个对应于查询的学生的信息。

如果没有对应的学生信息,则输出 “No Answer !”

样例输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
5
001 张三 男 19
002 李四 男 20
003 王五 男 18
004 赵六 女 17
005 刘七 女 21
7
003
002
005
004
003
001
006

样例输出

1
2
3
4
5
6
7
003 王五 男 18
002 李四 男 20
005 刘七 女 21
004 赵六 女 17
003 王五 男 18
001 张三 男 19
No Answer!

题解

思路

这题其实很简单,立马想到了C++的结构体,JAVA中就是类,一开始用的内部类,但是由于JAVA基础都忘的差不多了,所以就运行出错了,然后就用了几个数组,需要注意的是,JAVA中使用sc.nextInt()下一次使用sc.nextLine()会读不出内容,原因是nextLine()读进去了\n,因此最好统一输入方式,然后使用类型转换即可,比如Integer.parseInt()

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

while (scanner.hasNext()) {
// 输入
int n = Integer.parseInt(scanner.nextLine());
String id[] = new String[n + 1];
String name[] = new String[n + 1];
String sex[] = new String[n + 1];
int age[] = new int[n + 1];
for (int i = 0; i < n; i++) {
String temp = scanner.nextLine();
String[] tempString = temp.split(" ");

id[i] = tempString[0];
name[i] = tempString[1];
sex[i] = tempString[2];
age[i] = Integer.parseInt(tempString[3]);
}
int m = Integer.parseInt(scanner.nextLine());
// 外层遍历输入的学号
for (int i = 0; i < m; i++) {
String tar = scanner.nextLine();
int j = 0;
for (; j < n; j++) {
if (id[j].equals(tar)) {
System.out.println(id[j] + " " + name[j] + " " + sex[j] + " " + age[j]);
break;
}
}
if (j == n) {
System.out.println("No Answer!");
}
}
}
}
}

问题 D: 查找

题目

题目描述

输入数组长度 n 输入数组 a[1...n] 输入查找个数 m 输入查找数字 b[1...m] 输出 YES or NO 查找有则 YES 否则 NO 。

输入

输入有多组数据。 每组输入 n,然后输入 n 个整数,再输入 m,然后再输入 m 个整数( 1<= m <= n <= 100 )。

输出

如果在 n 个数组中输出 YES 否则输出 NO。

样例输入

1
2
3
4
6
3 2 5 4 7 8
2
3 6

样例输出

1
2
YES
NO

题解

思路

跟之前的思路差不多。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);
// 输入
while (scanner.hasNext()) {
int n = scanner.nextInt();
int num[] = new int[n + 1];
for (int i = 0; i < n; i++) {
num[i] = scanner.nextInt();
}
// 查找
int m = scanner.nextInt();
for (int i = 0; i < m; i++) {
int temp = scanner.nextInt();
int j = 0;
for (; j < n; j++) {
if (temp == num[j]) {
System.out.println("YES");
break;
}
}
if (j == n) {
System.out.println("NO");
}
}
}
}
}

问题 E: 学生查询

题目

题目描述

输入 n 个学生的信息,每行包括学号、姓名、性别和年龄,每一个属性使用空格分开。最后再输入一学号,将该学号对应的学生信息输出。

输入

测试数据有多组,第一行为样例数 m。对于每个样例,第一行为学生人数 n ( n 不超过 20 ),加下来 n 行每行 4 个整数分别表示学号、姓名、性别和年龄,最后一行表示查询的学号。

输出

输出 m 行,每行表示查询的学生信息,格式参见样例。

样例输入

1
2
3
4
5
6
7
1
4
1 李江 男 21
2 刘唐 男 23
3 张军 男 19
4 王娜 女 19
2

样例输出

1
2 刘唐 男 23

题解

思路

输入每个样例再进行操作,不要理解错误。也是很简单的。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.Scanner;

public class Main {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int m = Integer.parseInt(scanner.nextLine());
for (int k = 0; k < m; k++) {
int n = Integer.parseInt(scanner.nextLine());
String id[] = new String[n + 1];
String name[] = new String[n + 1];
String sex[] = new String[n + 1];
String age[] = new String[n + 1];
// 输入
for (int i = 0; i < n; i++) {
String temp = scanner.nextLine();
String tempString[] = temp.split(" ");
for (int j = 0; j < m; j++) {
id[i] = tempString[0];
name[i] = tempString[1];
sex[i] = tempString[2];
age[i] = tempString[3];
}
}
String tar = scanner.nextLine();
for (int i = 0; i < n; i++) {
if (tar.equals(id[i])) {
System.out.println(id[i] + " " + name[i] + " " + sex[i] + " " + age[i]);
}
}
}
}
}

《算法笔记》3.2小节——入门模拟->查找元素
https://excelius.xyz/《算法笔记》3-2小节——入门模拟-查找元素/
作者
Ther
发布于
2021年11月11日
许可协议