说明:以下介绍三种方法,三者的区别仅在于文件读取方式不同:
方法1直接把.txt文件复制粘贴到console控制台上,使用了Scanner类
方法2和方法3都是从电脑文件夹读取文件,但是具体读取细节有些差别
方法2使用了io接口的三个包:File,Reader,FileReader包
方法3使用了io接口的BufferedReader,InputStreamReader,FileInputStream来读取文件,还有String类自带的equals方法来比较字符串2020
最后:三个方法 解题思路都是一致的,就是暴力遍历矩阵
1.代码如下
1)方法1:纯暴力遍历矩阵,符合要求的就计数
import java.util.Scanner;
public class Find2020 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
char[][] ch = new char[300][300]; //打开.txt文件,把光标移到最后一个数字(最后一行最后一列的那个数)的前面,可以看到矩阵是多少行多少列的
long sum = 0;
for (int i = 0; i < ch.length; i++) {
ch[i] = s.nextLine().toCharArray();
}
for (int i = 0; i < ch.length; i++) {
for (int j = 0; j < ch[0].length; j++) {
if (j + 3 < ch[0].length) { // Traverse every line
if (ch[i][j] == '2' && ch[i][j + 1] == '0' && ch[i][j + 2] == '2' && ch[i][j + 3] == '0')
sum++;
}
if (i + 3 < ch.length && j < ch.length) { // Traverse every column
if (ch[i][j] == '2' && ch[i + 1][j] == '0' && ch[i + 2][j] == '2' && ch[i + 3][j] == '0')
sum++;
}
if (j + 3 < ch[0].length && i + 3 < ch.length) { //Traverse by diagonal
if (ch[i][j] == '2' && ch[i + 1][j + 1] == '0' && ch[i + 2][j + 2] == '2'
&& ch[i + 3][j + 3] == '0')
sum++;
}
}
}
System.out.println("So there are " + sum + " of 2020 in the .txt.");
}
2)方法2:暴力遍历
方法2:和方法一解题思一致,就是纯遍历,只是.txt文件读取方式不一样了,这里用reader直接从磁盘读取文件,省去复制粘贴.txt文件了
import java.io.File;
import java.io.Reader;
import java.io.FileReader;
public class B_Find2020 {
public static void main(String[] args) throws Exception { // 异常抛出,不处理
int sum = 0; // 记录2020的个数
char[][] ch = new char[300][300];
// 第1步、使用File类找到一个文件
File f = new File( // 声明File对象,File.separator是啥意思可以自己百度
"C:" + File.separator + "Users" + File.separator + "Administrator" + File.separator + "Desktop"
+ File.separator + "OldExamsofLanQiao" + File.separator + "JB202011th" + File.separator + "2020.txt");
// 第2步、通过子类实例化父类对象
Reader input = null; // 准备好一个输入的对象
input = new FileReader(f); // 通过对象多态性,进行实例化
// 第3步、进行读操作
for (int i = 0; i < 300; i++) { // 读取每一行,把读取的内容直接存到矩阵mtrx里面
input.read(ch[i]);
}
// 第4步、关闭输出流
input.close(); // 关闭输出流
for (int i = 0; i < ch.length; i++) {
for (int j = 0; j < ch[0].length; j++) {
if (j + 3 < ch[0].length) { // 每行
if (ch[i][j] == '2' && ch[i][j + 1] == '0' && ch[i][j + 2] == '2' && ch[i][j + 3] == '0')
sum++;
}
if (i + 3 < ch.length && j < ch.length) { // 每列
if (ch[i][j] == '2' && ch[i + 1][j] == '0' && ch[i + 2][j] == '2' && ch[i + 3][j] == '0')
sum++;
}
if (j + 3 < ch[0].length && i + 3 < ch.length) { // 对角线
if (ch[i][j] == '2' && ch[i + 1][j + 1] == '0' && ch[i + 2][j + 2] == '2'
&& ch[i + 3][j + 3] == '0')
sum++;
}
}
}
// 输出结果
System.out.println(sum);
}
}
3)方法3:继续暴力
//方法3:用io流读入.txt文件,和方法2的读取方式有稍微的差别,直接用String类自带的equals方法来比较字符串,
//转载于文章https://blog.csdn.net/hailei1/article/details/109342730
import java.io.*;
public class B_Find2020{
public static void main(String[] args) throws IOException {
//step1:文件读取
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(
"C:\\Users\\Administrator\\Desktop\\OldExamsofLanQiao\\JB202011th\\2020.txt")));
String[] str = new String[300];
for (int i = 0; i < 300; i++) {
if (reader.ready()) {
str[i] = reader.readLine();
}
}
//step2:定义并将读取的文件内容输入到矩阵里
String[][] str_matrix = new String[300][301];
for (int i = 0; i < 300; i++) {
for (int j = 0; j < 300; j++) {
str_matrix[i][j]=str[i].substring(j,j+1);
}
}
//step3:遍历查找每行每列每个对角线的2020
int sum = 0; //记录找到的2020的数目
int rowLen= str_matrix[0].length; //行的长度,记录矩阵每行有多少元素(或者说记录矩阵有多少列)
int colLen= str_matrix.length; //列的长度,记录矩阵每列有多少元素(或者说记录矩阵有多少行)
for (int i = 0; i < colLen; i++) { //遍历每一行
for (int j = 0; j < rowLen - 3; j++) {
if ((str_matrix[i][j]+str_matrix[i][j + 1]+str_matrix[i][j + 2]+str_matrix[i][j + 3]).equals("2020")) { //注意,不能用==来判断字符串是否相等,要用equlas方法判断
sum++;
}
}
}
for (int i = 0; i < colLen - 3; i++) { //遍历每一列
for (int j = 0; j < rowLen; j++) {
if ((str_matrix[i][j]+str_matrix[i + 1][j]+str_matrix[i + 2][j]+str_matrix[i + 3][j]).equals("2020")) {
sum++;
}
}
}
for (int i = 0; i < colLen - 3; i++) { //遍历对角线
for (int j = 0; j < rowLen - 3; j++) {
if ((str_matrix[i][j]+str_matrix[i + 1][j + 1]+str_matrix[i + 2][j + 2]+str_matrix[i + 3][j + 3]).equals("2020")) {
sum++;
}
}
}
System.out.print(sum);
}
}
4)方法4:用滑动窗口,待学习后补充
2.运行结果
1)方法1结果
2)方法2结果
和方法一是一致的
3)方法3结果
和方法一是一致的
注:小白一枚,以上如有错误,还请大佬不吝赐教,不胜感激~
附:2020.txt文件内容
链接: 点击跳转.