目录
一、Java基础3 -- 课后习题解析:
1、)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int c = 0, r = 0, f = 0,tot = 0;
while(n -- > 0){//相当于if(n>0)则执行循环体的语句然后n--
int a = sc.nextInt();
String t = sc.next();
tot += a;
if("C".equals(t)) c += a;//相当于if(t='C')
else if("R".equals(t)) r+=a;
else f += a;
}
System.out.printf("Total: %d animals\n",tot);
System.out.printf("Total coneys: %d\n",c);
System.out.printf("Total rats: %d\n",r);
System.out.printf("Total frogs: %d\n",f);
System.out.printf("Percentage of coneys: %.2f %%\n",c * 100.0 / tot);
System.out.printf("Percentage of rats: %.2f %%\n",r * 100.0 / tot);
System.out.printf("Percentage of frogs: %.2f %%\n",f * 100.0 / tot);
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while (n -- > 0) {
int x = sc.nextInt(), y = sc.nextInt();
if (x > y) {
int t = x;
x = y;
y = t;
}
int sum = 0;
for (int i = x + 1; i < y; i ++ )
if (i % 2 != 0)
sum += i;
System.out.println(sum);
}
}
}
代码有什么地方不明白的可以指出来
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(); // N 个测试用例
while(n -- > 0) {
int x = sc.nextInt();
int sum = -x; //除了本身以外的其他约数的和
for(int i = 1;i <= x / i;i ++) {
if(x % i == 0) { //则为约数
sum += i;
if(i != x / i) sum += x/i; //若x为完全平方数,则会重复计算
}
}
if(sum == x)
System.out.printf("%d is perfect\n",x);
else
System.out.printf("%d is not perfect\n",x);
}
}
}
二、一维数组
1.1 数组的定义
数组的定义方式和变量类似。
public class Main {
public static void main(String[] args) {
int[] a = new int[10], b;
float[] f = new float[33];
double[] d = new double[123];
char[] c = new char[21];
}
}
1.2 数组的初始化
public class Main {
public static void main(String[] args) {
int[] a = {0, 1, 2}; // 含有3个元素的数组,元素分别是0, 1, 2
int[] b = new int[3]; // 含有3个元素的数组,元素的值均为0
char[] d = {'a', 'b', 'c'}; // 字符数组的初始化
}
}
1.3 访问数组元素
通过下标访问数组。
public class Main {
public static void main(String[] args) {
int[] a = {0, 1, 2}; // 数组下标从0开始System.out.printf("%d %d %d\n", a[0], a[1], a[2]);
a[0] = 5;
System.out.println(a[0]);
}
}
练习题1: 使用数组实现求斐波那契数列的第 N 项。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();int[] f = new int[n + 1];
f[0] = 0;
f[1] = 1;
for (int i = 2; i <= n; i ++ )
f[i] = f[i - 1] + f[i - 2];System.out.println(f[n]);
}
}
练习题2:输入一个 n,再输入 n 个整数。将这 n 个整数逆序输出。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();int[] a = new int[n];
for (int i = 0; i < n; i ++ )
a[i] = sc.nextInt();for (int i = n - 1; i >= 0; i -- )
System.out.printf("%d ", a[i]);
}
}
练习题3:输入 n 个数,将这 n 个数按从小到大的顺序输出。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i ++ )
a[i] = sc.nextInt();for (int i = 0; i < n; i ++ )
for (int j = i + 1; j < n; j ++ )
if (a[i] > a[j]) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}for (int i = 0; i < n; i ++ )
System.out.printf("%d ", a[i]);
}
三、多维数组
多维数组就是数组的数组。
public class Main {
public static void main(String[] args) {
int[][] a = new int[3][4]; // 大小为3的数组,每个元素是含有4个整数的数组。
int[][][] b = new int[10][20][30]; // 将所有元素的初值为0
// 大小为10的数组,它的每个元素是含有20个数组的数组
// 这些数组的元素是含有30个整数的数组
}
}
public class Main {
public static void main(String[] args) {
int[][] a = { // 三个元素,每个元素都是大小为4的数组
{0, 1, 2, 3}, // 第1行的初始值
{4, 5, 6, 7}, // 第2行的初始值
{8, 9, 10, 11} // 第3行的初始值
};
for (int i = 0; i < 4; i ++ ) // 将第一行全部变成0
a[0][i] = 0;for (int i = 0; i < 3; i ++ ) { // 输出二维数组
for (int j = 0; j < 4; j ++ ) {
System.out.printf("%d ", a[i][j]);
}
System.out.println();
}
}
}
四、 数组的范围遍历
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int[][] a = {
{0, 1, 2, 3},
{4, 5, 6, 7},
{8, 9, 10, 11},
};for (int[] row: a) { // 范围遍历
for (int x: row) // 范围遍历
System.out.printf("%d ", x);
System.out.println();
}
}
}
五、常用API
- 属性length:返回数组长度,注意不加小括号
- Arrays.sort():数组排序
- Arrays.fill(int[] a, int val):填充数组
- Arrays.toString():将数组转化为字符串
- Arrays.deepToString():将多维数组转化为字符串
- 数组不可变长
- 使用Arrays需要import java.util.Arrays
课后习题
一、
输入一个二维数组 M[12][12]M[12][12],根据输入的要求,求出二维数组的下方区域元素的平均值或元素的和。
数组的两条对角线将数组分为了上下左右四个部分,如下图所示,黄色部分为对角线,绿色部分为下方区域:
输入格式
第一行输入一个大写字母,若为 S,则表示需要求出下方区域的元素的和,若为 M,则表示需要求出下方区域的元素的平均值。
接下来 12 行,每行包含 12 个用空格隔开的浮点数,表示这个二维数组,其中第 i+1 行的第 j+1 个数表示数组元素 M[i][j]。
输出格式
输出一个数,表示所求的平均数或和的值,保留一位小数。
数据范围
−100.0 ≤ M[i][j] ≤ 100.0
输入样例:
S
-6.0 0.7 -8.4 -5.7 -4.1 7.6 9.5 -9.7 4.1 0.6 -6.5 -4.9
6.6 4.9 -3.1 5.3 0.3 -4.5 3.9 -1.5 6.6 7.0 5.1 2.5
-8.5 1.8 -2.7 0.1 -4.9 -7.2 4.3 6.0 -1.4 2.7 -3.0 2.0
4.8 -7.0 -1.3 0.8 1.0 4.5 -1.1 -2.9 -3.9 -3.9 -8.9 5.8
-2.1 -9.6 5.1 0.2 1.0 -1.7 6.4 4.1 2.8 -6.9 2.4 9.3
-6.0 -9.1 -7.0 -7.0 7.8 5.1 6.9 -7.6 0.4 -7.2 5.5 6.0
-1.9 5.5 1.9 -8.5 -5.3 2.3 -9.3 2.0 -0.2 1.2 5.6 -1.8
8.2 2.3 3.5 1.4 4.0 -5.1 -6.9 -2.8 1.7 -7.0 7.8 1.8
-6.0 -4.1 -4.6 -9.4 -4.9 -4.1 4.2 6.3 -2.8 8.7 8.1 -0.9
8.8 -6.5 -4.3 6.1 -6.2 -3.9 -7.0 7.3 5.0 -0.9 -0.0 5.6
-2.4 1.4 8.5 -2.2 0.9 5.3 3.6 8.8 -8.1 3.0 -3.1 6.5
-3.8 -6.4 2.3 4.2 -9.8 -0.3 -9.9 -7.4 3.5 1.5 -0.2 7.0
输出样例:
-11.9
二、
输入整数 N,输出一个 N 阶的二维数组 M。
这个 N 阶二维数组满足 M[i][j]=2的i+j次方。
具体形式可参考样例。
输入格式
输入包含多行,每行包含一个整数 N。
当输入行为 N=0 时,表示输入结束,且该行无需作任何处理。
输出格式
对于每个输入整数 N,输出一个满足要求的 N 阶二维数组。
每个数组占 N 行,每行包含 N 个用空格隔开的整数。
每个数组输出完毕后,输出一个空行。
数据范围
0 ≤ N ≤ 150
输入样例:
1
2
3
4
5
0
输出样例:
1 1 2 2 4 1 2 4 2 4 8 4 8 16 1 2 4 8 2 4 8 16 4 8 16 32 8 16 32 64 1 2 4 8 16 2 4 8 16 32 4 8 16 32 64 8 16 32 64 128 16 32 64 128 256
Tips:课后习题题解在下一节文章开头部分~