1. 本次实验提供了一些代码示例,对于部分题目,只需要进行代码填空即可——将 "?" 或 "// TODO" 替换成正确的代码。
2. 条件语句和循环语句(如if, for, while等)只讲紧跟着的一条语句作为它的循环体,这条语句可以是
- 由一个 ; 分隔的简单语句
- 由花括号括起来的复合语句
60005 排序
可以参考书中冒泡排序或插入排序的相关内容。
60006 简化的插入排序
1. 测试数据
1 2 3 4 5
6
1 2 3 4 5
5
1 2 3 4 5
0
1 2 3 4 5
1
1 2 3 4 5
3
2. 代码
import java.util.Scanner;
public class SimplerSort {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int repeat = in.nextInt();
while (repeat -- != 0) {
// Input
int[] a = new int[5];
for (int i = 0; i < 5; ++ i)
a[i] = in.nextInt();
int num = in.nextInt();
// Output
int idx = 0;
// Output a[idx] which is less than num
for (idx = 0; ? && ?; ++ idx)
?
// Output num
?
// Output a[idx] which is NOT less than num
for (; ?; ++ idx)
?
System.out.println();
}
in.close();
}
}
60007 查找指定元素
60012 加法口诀表
import java.util.Scanner;
public class Add {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int repeat = in.nextInt();
for (int ri = 1; ri <= repeat; ++ri) {
int n = in.nextInt();
int[][] a =