[Java] 实验11

本次Java实验主要涉及条件语句和循环语句的使用,包括排序算法、查找指定元素、矩阵操作等实战练习。实验提供了部分代码示例,要求填充正确代码,例如冒泡排序、简化插入排序等。同时,给出了不同场景的测试数据,以增强对循环控制的理解和应用。

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 = new int[n + 1][n + 1];
      // Initialization
      for (int i = 0; i < n + 1; ++i)
        a[i][0] = ?;  // 初始化第0列的值
      for (int j = 0; j < n + 1; ++j)
        a[0][j] = ?;  // 初始化第0排的值
      // Calculation
      for (int i = 1; i < n + 1; ++i)
        for (int j = 1; j < n + 1; ++j)
          a[i][j] = ?;  // 计算a[i][j]的值
      // Print the result
      for (int i = 0; i <= n; ++i) {
        for (int j = 0; j <= n; ++j) {
          if (i == 0 && j == 0) {
            System.out.print("+   ");
          } else if (i == 0 || j <= i) {
            System.out.print(a[i][j] + "   ");
          }
        }
        System.out.println();        
      }
    }
    in.close();
  }
}

60013 判断上三角矩阵

import java.util.Scanner;

public class UpperTriangular {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int repeat = in.nextInt();
    while (repeat -- != 0) {
      int n = in.nextInt();
      int[][] a = new int[n][n];
      for (int i = 0; i < n; ++ i)
        for (int j = 0; j < n; ++ j)
          a[i][j] = in.nextInt();
      boolean isUpperTriangular = true;
      for (int i = 0; i < ? && isUpperTriangular; ++ i)
        for (int j = 0; j < ? && isUpperTriangular; ++ j)
          ? // Modify isUpperTriangular here if necessary
      System.out.println(isUpperTriangular? "YES": "NO");
    }
    in.close();
  }
}

60014 求矩阵每行元素之和

import java.util.Scanner;

public class SwapRows {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int repeat = in.nextInt();
    while (repeat-- != 0) {
      // Input the matrix
      int[][] a = new int[3][3];
      for (int i = 0; i < 3; ++ i)
        for (int j = 0; j < 3; ++ j)
          a[i][j] = in.nextInt();
      // Swap the first and the last row
      for (int j = 0; j < 3; ++ j) {
        // TODO: swap a[0][j] and a[2][j] here
      }
      // Output the matrix
      for (int i = 0; i < 3; ++ i)
        for (int j = 0; j < 3; ++ j)
          System.out.println(a[i][j] + '\t');
    }
    in.close();
  }
}

60015 交换矩阵中的两行

import java.util.Scanner;

public class SwapRows {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int repeat = in.nextInt();
    while (repeat-- != 0) {
      int[][] a = new int[3][3];
      for (int i = 0; i < 3; ++ i)
        for (int j = 0; j < 3; ++ j)
          a[i][j] = in.nextInt();
      for (int j = 0; j < 3; ++ j) {
        // TODO: swap a[0][j] and a[2][j] here
      }
    }
    in.close();
  }
}

60019 找鞍点

import java.util.Scanner;

public class SaddlePoint {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int repeat = in.nextInt();
    while (repeat -- != 0) {
      // Input
      int n = in.nextInt();
      int[][] a = new int[n][n];
      for (int i = 0; i < n; ++ i)
        for (int j = 0; j < n; ++ j)
          a[i][j] = in.nextInt();
      // Initialization
      boolean foundSaddlePoint = false;
      int row = -1, col = -1;
      // Find the saddle point
      for (int i = 0; i < n && !foundSaddlePoint; ++ i)
        for (int j = 0; j < n && !foundSaddlePoint; ++ j)
          if (foundSaddlePoint = saddlePoint(a, i, j)) {
            row = ?;
            col = ?;
          }
      // Output the result
      if (foundSaddlePoint)
        System.out.println("a[" + row + "][" + col + "]=" + a[row][col]);
      else
        System.out.println("NO");
    }
    in.close();
  }

  private static boolean saddlePoint(int[][] a, int i, int j) {
    return ? && ?
  }

  // Determine whether a[i][j] is the largest element in the row
  private static boolean largestInRow(int[][] a, int i, int j) {
    ?
  }
  
  // Determine whether a[i][j] is the least element in the column
  private static boolean leastInColumn(int[][] a, int i, int j) {
    ?
  }
}

完整代码:

import java.util.Scanner;

public class SaddlePoint {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int repeat = in.nextInt();
    while (repeat -- != 0) {
      // Input
      int n = in.nextInt();
      int[][] a = new int[n][n];
      for (int i = 0; i < n; ++ i)
        for (int j = 0; j < n; ++ j)
          a[i][j] = in.nextInt();
      // Initialization
      boolean foundSaddlePoint = false;
      int row = -1, col = -1;
      // Find the saddle point
      for (int i = 0; i < n && !foundSaddlePoint; ++ i)
        for (int j = 0; j < n && !foundSaddlePoint; ++ j)
          if (foundSaddlePoint = saddlePoint(a, i, j)) {
            row = i;
            col = j;
          }
      // Output the result
      if (foundSaddlePoint)
        System.out.println("a[" + row + "][" + col + "]=" + a[row][col]);
      else
        System.out.println("NO");
    }
    in.close();
  }

  private static boolean saddlePoint(int[][] a, int i, int j) {
    return largestInRow(a, i, j) && leastInColumn(a, i, j);
  }

  // Determine whether a[i][j] is the largest element in the row
  private static boolean largestInRow(int[][] a, int i, int j) {
    for (int num: a[i])
      if (num > a[i][j])
        return false;
    return true;
  }
  
  // Determine whether a[i][j] is the least element in the column
  private static boolean leastInColumn(int[][] a, int i, int j) {
    for (int k = 0; k < a.length; ++ k)
      if (a[k][j] < a[i][j])
        return false;
    return true;
  }
}


60032 编写函数求矩阵最大值

// 1. 函数定义
private static int max(int[][] a) {
  // TODO  
}

// 2. 遍历矩阵
for (int i = 0; i < a.length; ++ i)
  for (int j = 0; j < a.length; ++ j)
    // TODO

60033 矩阵相加

import java.util.Scanner;

public class MatrixAdd {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int repeat = in.nextInt();
    while (repeat -- != 0) {
      int n = in.nextInt();
      int[][] a = new int[n][n];
      int[][] b = new int[n][n];
      int[][] c = new int[n][n];
      for (int i = 0; i < n; i ++ )
        for (int j = 0; j < n; j ++ )
          a[i][j] = in.nextInt();
      for (int i = 0; i < n; i ++ )
        for (int j = 0; j < n; j ++ )
          b[i][j] = in.nextInt();
      add(a, b, c);
      prt("A Matrix", a);
      prt("B Matrix", b);
      prt("A+B Matrix", c);
    }
    in.close();
  }

  private static void add(int[][] a, int[][] b, int[][] c) {
    // TODO
  }
  
  private static void prt(String prompt, int[][] matrix) {
    // Print the prompt first
    // TODO
    
    // Then print the matrix
    // TODO
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值