[Java] 实验12

60021 求字符串长度

import java.util.Scanner;

public class StringLengths {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    // Obtain the string, like "hello#123#" , from the input
    String str = in.next();
    int len = 0;
    // Iterate the whole string
    for (int i = 0; i < str.length(); ++ i) {
      if (?) {
        ?
      } else {
        ?
      }
    }
    in.close();
  }
}

60022 统计大写辅音字母

大写的。


60023 查找字符

可以参考 60021 的代码。


60024 字符串替换

1. 找规律。假设有替换前字母 a1, 替换后字母 a2, 它们满足:

    a1 + a2 == 'A' + 'Z'

2. 如何遍历字符串。可以参考60021.

代码填空:

import java.util.Scanner;

public class StringReplacement {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int repeat = (in.nextLine()).charAt(0) - '0';
    while (repeat-- != 0) {
      String str = in.nextLine();
      for (int i = 0; i < str.length(); ++ i) {
        if (?) {
          // If str.charAt(i) is an upper character, output the character after replacement
          System.out.print(?);
        } else {
          // If not, Just output the original character
          System.out.print(?);
        }
      }
      // Output an enter(回车)
      System.out.println();
    }
    in.close();
  }
}
解法一:
import java.util.Scanner;

public class StringReplacement2 {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int repeat = (in.nextLine()).charAt(0) - '0';
    while (repeat-- != 0) {
      String str = in.nextLine();
      String str2 = new String();
      for (int i = 0; i < str.length(); ++ i)
        if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')
          str2 += (char)('A' + 'Z' - str.charAt(i));
        else
          str2 += str.charAt(i);
      System.out.println(str2);
    }
    in.close();
  }
}
解法二(使用问号表达式):
import java.util.Scanner;

public class StringReplacement {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int repeat = (in.nextLine()).charAt(0) - '0';
    while (repeat-- != 0) {
      String str = in.nextLine();
      for (int i = 0; i < str.length(); ++ i)
        System.out.print(str.charAt(i) >= 'A' && str.charAt(i) <= 'Z'?
            (char)('A' + 'Z' - str.charAt(i)): str.charAt(i));
      System.out.println();
    }
    in.close();
  }
}

60025 十六进制转换十进制

import java.util.Scanner;

public class HexToDec {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int repeat = in.nextInt();
    while (repeat-- != 0) {
      String str = in.next();
      int number = 0;
      for (int i = 0; i < str.length(); ++ i)
        if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
          // num << 4, 左移 4 位,亦即 * 16
          number = (number << 4) + str.charAt(i) - '0';
        } else if (str.charAt(i) >= 'a' && str.charAt(i) <= 'f') {
          number = (number << 4) + str.charAt(i) - 'a' + 10;
        } else if (str.charAt(i) >= 'A' && str.charAt(i) <= 'F') {
          number = (number << 4) + str.charAt(i) - 'A' + 10;
        }
      System.out.println(number);
    }
    in.close();
  }
}

60026 将字符串逆序输出

1. 逆序访问字符串中的每个字符

for (int i = str.length() - 1; i >= 0; -- i) {
  // TODO
}

2. 为什么在此需要用 in.next() 而非 in.nextLine() 读取字符串

考虑我们有如下程序:

import java.util.Scanner;

public class ScannerTest {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int repeat = in.nextInt();
    String line = in.nextLine();
    System.out.println("First line: " + line);
    line = in.nextLine();
    System.out.println("Second line: " + line);
  }
}
大家需要将输入,想想成一个输入流(input stream). 是被用户一点一点读取的。我们在此先解释下述三个方法的作用:

    - nextInt(): 从输入流中,读取一段字符串,将其解析成一个整型;

    - next(): 从输入流中,忽略前导的空格和回车,读取一个字符串,碰到空格或回车停止(并把这个回车从输入流中丢弃);

    - nextLine(): 从输入流中,读取一行,碰到回车停止(并把这个回车从输入流中丢弃)


考虑我们有如下的输入:

3
This line will be read by the second in.nextLine().

所以我们的输入流是 :“5回车This line will be read by the second in.nextLine().回车


程序执行到第 5 行,in.nextInt() 将读取这个数字 3, 并把它赋值给 repeat. 注意到,这时输入流是:“回车This line will be read by the second in.nextLine().回车

程序执行到第 7 行,in.nextLine() 将读取到回车,并把回车丢弃,把一个空字符串赋值给 line.

程序执行到第 9 行,in.nextLine() 将读取到"This line will be read by the second in.nextLine().回车"并丢弃掉这个回车。

所以输出结果会是:

First line: 
Second line: This line will be read by the second in.nextLine().

但是,如果我们在此,不用 in.nextLine(), 而选用 in.next() 就可以以回车或空格为终结,每次读取一个字符串了。仍不理解的同学,可以复制上述代码(或者测试一下 in.next() 方法),在 eclipse 中予以实验,毕竟实践出真知。

考试中应该选用 next() 还是 nextLine(), 若题目提供了编程模板,那就可以按它提供的写;若没有的话,就需要大家理解这两个方法的功能,并根据题意选择了。


60030 编写排序函数(方法)

下面给出了对应两种排序的参考链接,大家可以通过阅读该链接 或 查找相关中文文献,以了解这两种排序算法的原理。

最后再独立实现编码(独立实现非常重要)。

冒泡排序 (bubble sort) 的参考链接:

    1. http://www.algolist.net/Algorithms/Sorting/Insertion_sort

    2. https://en.wikipedia.org/wiki/Insertion_sort

插入排序 (insertion sort) 的参考链接:

    1. http://www.algolist.net/Algorithms/Sorting/Insertion_sort

    2. https://en.wikipedia.org/wiki/Insertion_sort

import java.util.Scanner;

public class Sort {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int repeat = in.nextInt();
    while (repeat -- != 0) {
      int n = in.nextInt();
      int[] nums = new int[n];
      for (int i = 0; i < n; ++ i)
        nums[i] = in.nextInt();
      // You can alternatively call insertionSort or bubbleSort here.
      insertionSort(nums);
      // bubbleSort(nums);
      for (int num: nums)
        System.out.print(num + " ");
      System.out.println();
    }
    in.close();
  }
  
  // Reference:
  //   1. http://www.algolist.net/Algorithms/Sorting/Insertion_sort
  //   2. https://en.wikipedia.org/wiki/Insertion_sort
  private static void insertionSort(int[] nums) {
    for (int i = 1; i < nums.length; ++ i)
      for (int j = i; j - 1 >= 0; -- j)
        if (nums[j - 1] > nums[j])
          swap(nums, j - 1, j);
  }
  
  // Reference:
  //   1. http://www.algolist.net/Algorithms/Sorting/Bubble_sort
  //   2. https://en.wikipedia.org/wiki/Bubble_sort
  private static void bubbleSort(int[] nums) {
    boolean hasSwapped = true;
    // This loop will terminate when there is NO swap operation happens during
    // the whole iterations of an array.
    while (hasSwapped) {
      hasSwapped = false;
      for (int i = 0; i < nums.length - 1; ++ i)
        if (nums[i] > nums[i + 1]) {
          hasSwapped = true;
          swap(nums, i, i + 1);
        }
    }
  }

  // Swap the two elements, nums[i] and nums[j], in the array -- nums
  private static void swap(int[] nums, int i, int j) {
    int temp = nums[i];
    nums[i] = nums[j];
    nums[j] = temp;
  }
}

60031 判断两个矩阵是否相同(方法)

解法一:

import java.util.Scanner;  
  
public class MatrixEquals {  
  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];
      input(a, in);  
      input(b, in);  
      System.out.println(judge(a, b)? "Yes": "No");  
    }  
    in.close();  
  }  
    
  private static void input(int[][] nums, Scanner in) {  
    for (int i = 0; i < nums.length; ++ i)  
      for (int j = 0; j < nums[i].length; ++ j)  
        nums[i][j] = in.nextInt();  
  }  
    
  private static boolean judge(int[][] a, int[][] b) {  
    for (int i = 0; i < a.length; ++ i)  
      for (int j = 0; j < a[i].length; ++ j)  
        if (a[i][j] != b[i][j]) return false;  
    return true;  
  }  
}

解法二(使用一维数组即可):

import java.util.Scanner;  
  
public class MatrixEquals2 {
  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];
      input(a, in);  
      input(b, in);  
      System.out.println(judge(a, b)? "Yes": "No");  
    }  
    in.close();  
  }  
    
  private static void input(int[] nums, Scanner in) {
    for (int i = 0; i < nums.length; ++ i)
      nums[i] = in.nextInt();
  }  
    
  private static boolean judge(int[] a, int[] b) {
    for (int i = 0; i < a.length; ++ i)
      if (a[i] != b[i]) return false;
    return true;
  }  
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值