题目
实验目的
熟悉Java常用类用法;复习与掌握基本控制结构。
实验要求
独立完成实验操作,并撰写实验报告(实验报告模板见附件)。
实验内容
-
使用Math类产生随机数
构造拥有n个元素的数组(n由键盘输入),往数组中随机填入2000-3000之间的整数,然后统计并输出其中大于2500与小于等于2500的百分比。(注意:可以使用for each循环进行统计,随机数用法见参考资料)
扩展:随机生成n个(n由键盘输入)从a-z的字符(26个字符),统计输出每个字符所占总生成字符的百分比。
扩展:附件Object.txt是java.lang.Object的JDK API文档,读取该文件,分别统计字符a-z出现的频率(百分比)。统计时仅统计字符a-z、A-Z,不分大小写,其余字符忽略。文件读取方法参考附件WriteReadFileTest.java。
随机数参考资料:(1)Math.random();//输出[0,1)的双精度随机数 (2)(int)(Math.random() * 10);//产生0到9之间的整数 (3)1 + (int)(Math.random() * 10);//产生[1,10]之间的整数 (4)(int)(3 + Math.random()*(17 - 3 + 1));//产生3到17之间的随机整数 (5)(char)('a' + Math.random() * ('z' - 'a' + 1)));//产生从a到z的随机字符 注意:如果要产生一系列的随机数,请使用java.util.Random类,查询jdk文档。
-
String对象的创建、截取和拼接
编写一函数public static String getBirthDate(String id),以YYYY-MM-DD抽取出给定id(身份证号码)的出生年月日信息,如:String id = "350102199902131018"; String result = getBirthDate(id);
result的值应为1999-02-13
进一步的,如给定若干身份证号(放到一数组中),抽取这些身份证号的出生年月日信息,并按从小到大的顺序或者从大到小的顺序排列输出抽取出的出生年月日。
思考:如果是对整个身份证号码,按照出生年月进行排序,如何实现?
提示:可直接使用Arrays类的sort(Object[] a)方法进行排序,无需自己编写排序算法。请同学们学会查询jdk文档。 -
使用Scanner类进行文件输入输出与字符串的处理
编写一个程序,实现从文件中读出学生信息,统计平均分、总分,并写入文件。
(1)阅读附件WriteReadFileTest.java关于文件读写的源代码。
(2)随机生成10000个学生及三门科目的分数,然后写入指定文件名的文件。文件格式如下(字段间的分隔符建议使用空格,简化输入输出,缺点是字段内不能含有空格):Name Math Java DS Average Total 张三 90 95 98 0 0 李四 80 90 100 0 0 ……
其中,姓名字段不需要随机生成,直接使用“张三xx”等即可;平均分(Average)与总分(Total)字段在随机生成时给0即可,后续再统计填充。
(3)从文件中读出学生信息,统计平均分、总分,并写入文件。比如,原学生信息为 “张三 90 91 92 0 0”,处理后的结果为"张三 90 91 92 91 273",这里仅使用整型运算即可,不需要用浮点数。
(4)找出平均成绩前10名的学生,写入“first_10.txt”文件。
(5)编写一个查找学生的方法,public static String[] findStudent(String name)。根据传入的name在文件中查找姓名相同的学生,并以数组的形式返回。 -
骑士巡游问题
在8×8方格的国际象棋棋盘上,马(也称为骑士Knight)从任意指定的方格出发,以跳马规则(横一步竖两步或横两步竖一步),周游棋盘的每一个格子,要求每个格子只能跳过一次。
(1)使用回溯算法,找出一组解(指定初始方格),并打印输出。
(2)在(1)的基础上,遍历所有可能解(指定初始方格),打印并统计解的个数。
(3)(选做)扩展为n×n棋盘,其中n由控制台输入,求其所有可能解(或无解时提示),打印并统计。
参考资料
WriteReadFileTest.java
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.Arrays;
public class WriteReadFileTest {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
//写入文件演示
PrintWriter out = new PrintWriter("myfile.txt");
out.println("姓名 高数 Java 数据结构 平均成绩 总成绩");
out.println("张三 20 30 40 0 0");
out.println("李四 50 60 70 0 0");
out.close();//输出完毕,需要close
//读入文件演示
Scanner in = new Scanner(new File("myfile.txt"));//为myfile.txt这个File创建一个扫描器in
int number = 1;//行号
while(in.hasNextLine()){//判断扫描器是否还有下一行未读取,该循环把文件的每一行都读出
String line = in.nextLine();//读出myfile.txt的下一行
System.out.println("第"+(number++)+"行的内容" +"="+line);
Scanner linescanner = new Scanner(line);//对每一行的内容建立一个扫描器
linescanner.useDelimiter(" ");//使用空格作为分隔符
String name = linescanner.next();
String math = linescanner.next();
String java = linescanner.next();
String ds = linescanner.next();
String avg = linescanner.next();
String total = linescanner.next();
System.out.println("name="+name+" math="+math+" java="+java+" ds="+ds+" avg"+avg+" total="+total);
}
in.close();//读入完毕,最后需要对其进行close。
//底下是参考实现代码,只进行简单的功能演示
String students[] = new String[100];
//创建100个学生
for(int i = 0 ; i<students.length ;i++){
students[i]=getStudent(i);
}
//输出每个学生的信息
for(String x:students){
System.out.println(x);
}
}
//获取学生信息,格式类似 "张三i 80 85 90 0 0"
public static String getStudent(int i){
String name = "张三"+i+" ";
String score = "";
for(int j = 0;j<3;j++){
score += getScore()+ " ";
}
return name+score+0+" "+0;
}
//获取0-100的随机数
public static int getScore(){
return (int)(Math.random()*101);
}
}
实验报告
目的
熟悉Java常用类用法;
复习与掌握基本控制结构。
二、实验内容与设计思想
设计思路
(1) 使用Math类产生随机数
(2) String对象的创建、截取和拼接
(3) 使用Scanner类进行文件输入输出与字符串的处理
(4) 骑士巡游问题
主要数据结构
问题一 | int[] | 存放随机数 |
char[] | 存放随机字符 | |
string | 存放文件读入的信息 | |
问题二 | string | 存放单个身份证号 |
ArrayList<String> | 存放多个String类型的身份证号 | |
问题三 | string[] | 存放单个学生信息 {姓名,成绩1,成绩2,成绩3,平均分,总分} |
ArrayList<String[]> | 存放多个String[]类型的学生信息 | |
问题四 | int[][] | 存放迷宫8×8的地图 |
int[][] | 存放8个2D的方向 |
主要代码结构
第一题:使用Math类产生随机数
1.private static void CreateRandomNumber() 需求一
2.private static void CreateRandomChar() 需求二
3.private static String readFile(String filename) 读取文件函数
4.private static void CountOfChar() 需求三
5.public static void main(String args[]) 主函数,调用三个函数,展示效果。
第二题:String对象的创建、截取和拼接
1.public static String getBirthDate(String id) 需求一
2.public static void SortBirthDate(ArrayList<String> birthDate) 需求二
3.public static void SortId(String[] id) 需求三
4.public static void main(String args[]) 主函数,调用三个函数,展示效果。
第三题:使用Scanner类进行文件输入输出与字符串的处理
1.public static void WriteFile(ArrayList<String[]> data, String path) 写文件函数
2.public static ArrayList<String[]> ReadFile() 读文件函数
3.public static void main(String[] args) 主函数,调用四个函数,展示效果。
4.public static void CreateStudent(int n) 需求一
5.public static void ModifyInformation() 需求二
6.public static void first_10() 需求三
7.public static String[] getStudent(int i) 生成学生信息
8.public static String[] findStudent(String name) 需求四
第四题:骑士巡游问题
1.public static void 输入棋盘大小
2.public static void print() 打印出路径
3.public static void init() 迷宫初始化
4.public static void dfs() 单次解_巡游深度优先算法
5.public static void dfs_all() 所有解_巡游深度优先算法
主要代码段分析
public static void dfs()
设置全局变量 |
|
设置结束的标志 |
|
优先遍历 |
|
三、实验使用环境
软件
java version “18.0.2”
EclipseIDE 2022-06
平台
WIN10
四、实验步骤和调试过程
4.1使用Math类产生随机数
需求
-
构造拥有n个元素的数组,往数组中随机填入2000-3000之间的整数,然后统计并输出其中大于2500与小于等于2500的百分比。
-
随机生成n个(n由键盘输入)从a-z的字符(26个字符),统计输出每个字符所占总生成字符的百分比。
-
读取附件,分别统计字符a-z出现的频率(百分比)。统计时仅统计字符a-z、A-Z,不分大小写,其余字符忽略。
实验步骤
-
private static void CreateRandomNumber() 需求一
-
private static void CreateRandomChar() 需求二
-
private static String readFile(String filename) 读取文件函数
-
private static void CountOfChar() 需求三
-
public static void main(String args[])
主函数,调用三个函数,展示效果。
代码分析
private static void CreateRandomNumber()
构造拥有n个元素的数组 |
|
往数组中随机填入2000-3000之间的整数 统计其中大于2500与小于等于2500的百分比 |
|
输出其中大于2500与小于等于2500的百分比 |
|
private static void CreateRandomChar()
随机生成n个
|
|
从a-z的字符(26个字符),统计每个字符所占总生成字符的百分比。 |
|
输出每个字符所占总生成字符的百分比 |
|
private static void CountOfChar()
读取附件 |
|
分别统计字符a-z出现的频率(百分比)。 |
|
输出字符a-z出现的频率(百分比) |
|
测试数据与结果
需求一 N=100 |
|
需求二 N=100 |
|
需求三 |
|
…… | …… |
4.2 String对象的创建、截取和拼接
需求
-
编写一函数public static String getBirthDate(String
id),以YYYY-MM-DD抽取出给定id(身份证号码)的出生年月日信息。 -
给定若干身份证号,抽取这些身份证号的出生年月日信息,并按从小到大的顺序或者从大到小的顺序排列输出抽取出的出生年月日。
-
对整个身份证号码,按照出生年月进行排序。
实验步骤
-
public static String getBirthDate(String id) 需求一
-
public static void SortBirthDate(ArrayList<String> birthDate)
需求二 -
public static void SortId(String[] id) 需求三
-
public static void main(String args[])
主函数,调用三个函数,展示效果。
代码分析
public static String getBirthDate(String id)
抽取年月日 |
|
按格式输出并返回 |
|
public static void SortBirthDate(ArrayList birthDate)
按从小到大的顺序或者从大到小的顺序排列抽取出的出生年月日。 |
|
输出抽取出的出生年月日 |
|
public static void SortId(String[] id)
按从小到大的顺序排列身份证号。 |
|
输出身份证号 |
|
测试数据与结果
身份证号是:350102199902131018 身份证号是:320124198808240056 身份证号是:371082197812141310 身份证号是:110108196711301866 身份证号是:211011198810284152 身份证号是:140424197907137271 | 出生年月日是:1999-02-13 出生年月日是:1988-08-24 出生年月日是:1978-12-14 出生年月日是:1967-11-30 出生年月日是:1988-10-28 出生年月日是:1979-07-13 |
需求二排序结果 | 1967-11-30 1978-12-14 1979-07-13 1988-10-28 1988-08-24 |
需求三排序结果 | 110108196711301866 371082197812141310 140424197907137271 320124198808240056 211011198810284152 |
…… | …… |
4.3使用Scanner类进行文件输入输出与字符串的处理
需求
-
随机生成10000个学生及三门科目的分数,然后写入指定文件名的文件。
-
从文件中读出学生信息,统计平均分、总分,并写入文件。
-
找出平均成绩前10名的学生,写入"first_10.txt"文件。
-
编写一个查找学生的方法,根据传入的name在文件中查找姓名相同的学生,并以数组的形式返回。
实验步骤
-
public static void WriteFile(ArrayList<String[]> data, String
path) 写文件函数 -
public static ArrayList<String[]> ReadFile() 读文件函数
public static void main(String[] args)
主函数,调用四个函数,展示效果。 -
public static void CreateStudent(int n) 需求一
-
public static void ModifyInformation() 需求二
-
public static void first_10() 需求三
-
public static String[] getStudent(int i) 生成学生信息
-
public static String[] findStudent(String name) 需求四
代码分析
public static void CreateStudent(int n) | |
随机生成10000个学生及三门科目的分数 |
|
写入指定文件名的文件 |
|
public static void ModifyInformation() | |
从文件中读出学生信息 |
|
统计平均分、总分 |
|
写入文件 |
|
public static void first_10() | |
读入文件 |
|
找出平均成绩前10名的学生, |
|
写入“first_10.txt”文件。 |
|
public static String[] findStudent(String name) | |
读入文件 |
|
根据传入的name在文件中查找姓名相同的学生,并 |
|
以数组的形式返回 | |
测试数据与结果
myfile.txt | 张三0 93 30 20 47 143 |
first_10.txt | 张三5639 99 95 98 97 292 |
…… | …… |
4.4骑士巡游问题
需求
-
使用回溯算法,找出一组解(指定初始方格),并打印输出。
-
在(1)的基础上,遍历所有可能解(指定初始方格),打印并统计解的个数。
-
扩展为n×n棋盘,其中n由控制台输入,求其所有可能解(或无解时提示),打印并统计。
实验步骤
-
public static void 输入棋盘大小
-
public static void print() 打印出路径
-
public static void init() 迷宫初始化
-
public static void dfs() 单次解_巡游深度优先算法
-
public static void dfs_all() 所有解_巡游深度优先算法
代码分析
public static void dfs() | |
设置全局变量 |
|
设置结束的标志 |
|
优先遍历 | |
测试数据与结果
输入棋盘大小 | N=5 |
单次巡游 | 单次巡游结果:-------------------------------------------- (0,0) ---> (1,2) ---> (2,4) ---> (0,3) ---> (1,1) ---> (3,0) ---> (4,2) ---> (3,4) ---> (1,3) ---> (0,1) ---> (2,0) ---> (4,1) ---> (3,3) ---> (1,4) ---> (2,2) ---> (4,3) ---> (3,1) ---> (1,0) ---> (0,2) ---> (2,1) ---> (4,0) ---> (3,2) ---> (4,4) ---> (2,3) ---> (0,4) |
所有巡游结果 | 第301次巡游结果:-------------------------------------------- (0,0) ---> (2,1) ---> (4,2) ---> (3,0) ---> (1,1) ---> (0,3) ---> (2,2) ---> (3,4) ---> (1,3) ---> (0,1) ---> (2,0) ---> (4,1) ---> (3,3) ---> (1,4) ---> (0,2) ---> (1,0) ---> (3,1) ---> (4,3) ---> (2,4) ---> (1,2) ---> (0,4) ---> (2,3) ---> (4,4) ---> (3,2) ---> (4,0) 第302次巡游结果:-------------------------------------------- (0,0) ---> (2,1) ---> (4,2) ---> (3,0) ---> (1,1) ---> (0,3) ---> (2,4) ---> (4,3) ---> (2,2) ---> (3,4) ---> (1,3) ---> (0,1) ---> (2,0) ---> (4,1) ---> (3,3) ---> (1,4) ---> (0,2) ---> (1,0) ---> (3,1) ---> (1,2) ---> (0,4) ---> (2,3) ---> (4,4) ---> (3,2) ---> (4,0) 第303次巡游结果:-------------------------------------------- (0,0) ---> (2,1) ---> (4,2) ---> (3,0) ---> (1,1) ---> (0,3) ---> (2,4) ---> (4,3) ---> (3,1) ---> (1,0) ---> (0,2) ---> (1,4) ---> (2,2) ---> (3,4) ---> (1,3) ---> (0,1) ---> (2,0) ---> (4,1) ---> (3,3) ---> (1,2) ---> (0,4) ---> (2,3) ---> (4,4) ---> (3,2) ---> (4,0) 第304次巡游结果:-------------------------------------------- (0,0) ---> (2,1) ---> (4,2) ---> (3,0) ---> (1,1) ---> (0,3) ---> (2,4) ---> (4,3) ---> (3,1) ---> (1,0) ---> (0,2) ---> (1,4) ---> (3,3) ---> (4,1) ---> (2,2) ---> (3,4) ---> (1,3) ---> (0,1) ---> (2,0) ---> (1,2) ---> (0,4) ---> (2,3) ---> (4,4) ---> (3,2) ---> (4,0) |
…… | …… |
五、实验小结
1.实验中遇到的问题及解决过程
没有什么问题。
实验中产生的错误及原因分析
编码中文格式显示问题:将txt另存为utf-8格式,而不是ANSI格式。
3.实验体会和收获。
对java的掌握更加熟练。
六、附录
代码过长,不在附录体现,放在附件中。
代码
ExtractInformation
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import static java.util.Arrays.*;
public class ExtractInformation {
//以YYYY-MM-DD抽取出给定id(身份证号码)的出生年月日信息
public static String getBirthDate(String id) {
System.out.printf("身份证号是:%s ", id);
String year = id.substring(6, 10), month = id.substring(10, 12), day = id.substring(12, 14);
String result = year + "-" + month + "-" + day;
System.out.printf("出生年月日是:%s\n", result);
return result;
}
//按从小到大的顺序或者从大到小的顺序排列输出抽取出的出生年月日
public static void SortBirthDate(ArrayList<String> birthDate) {
birthDate.sort(new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
String year1 = o1.substring(0,4), month1 = o2.substring(4,6), day1 = o1.substring(6,8);
String year2 = o2.substring(0,4), month2 = o2.substring(4,6), day2 = o2.substring(6,8);
if (year1.equals(year2)) {
if (month1.equals(month2))
return day1.compareTo(day2);
else
return month1.compareTo(month2);
} else
return year1.compareTo(year2);
}
});
System.out.println("出生年月排序结果:--------------------------------------------");
for (String i : birthDate)
System.out.print(i+"\n");
}
public static void SortId(String[] id) {
Arrays.sort(id,new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
String year1 = o1.substring(6,10), month1 = o2.substring(10,12), day1 = o1.substring(12,14);
String year2 = o2.substring(6,10), month2 = o2.substring(10,12), day2 = o2.substring(12,14);
if (year1.equals(year2)) {
if (month1.equals(month2))
return day1.compareTo(day2);
else
return month1.compareTo(month2);
} else
return year1.compareTo(year2);
}
});
System.out.println("身份证号排序结果:--------------------------------------------");
for (String i : id)
System.out.print(i+"\n");
}
public static void main(String args[]) {
String id = "350102199902131018";
String result = ExtractInformation.getBirthDate(id);
String[] ids = new String[]{"320124198808240056", "371082197812141310", "110108196711301866", "211011198810284152", "140424197907137271"};
ArrayList<String> birthDates = new ArrayList<String>();
for (String i : ids)
{
String birthData = getBirthDate(i);
birthDates.add(birthData);
}
ExtractInformation.SortBirthDate(birthDates);
ExtractInformation.SortId(ids);
}
}
RandomNumber
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class RandomNumber {
//构造拥有n个元素的数组(n由键盘输入),往数组中随机填入2000-3000之间的整数,然后统计并输出其中大于2500与小于等于2500的百分比。
private static void CreateRandomNumber() {
System.out.print("请输入n,随后将生成n个[2000,3000)的随机数。");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] num = new int[n];
int big = 0;
for (int i = 0; i < n; i++) {
num[i] = (int) (Math.random() * 1000 + 2000);
if (num[i] > 2500)
big++;
}
double ans = (double) big / n;
System.out.printf("大于2500的数字有%.2f\n", ans);
System.out.printf("小于等于2500的数字有%.2f\n", 1 - ans);
}
//随机生成n个从a-z的字符,统计输出每个字符所占总生成字符的百分比
private static void CreateRandomChar() {
System.out.print("请输入n,随后将生成n个a-z的字符。");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
char[] ch = new char[n];
int[] cnt = new int[26];
for (int i = 0; i < n; i++) {
int number = (int) (Math.random() * 26);
ch[i] = (char) (number + 97);
cnt[number]++;
}
for (int i = 0; i < 26; i++) {
System.out.printf("%c出现的次数占比为:%.3f%%\n", 'a' + i, (double) cnt[i] / (double) n * 100);
}
}
//读取文件
private static String readFile(String filename) throws IOException {
FileReader fileReader = new FileReader(filename);
BufferedReader bufferedReader = new BufferedReader(fileReader);
StringBuilder text = new StringBuilder();
String line = bufferedReader.readLine();
while (line != null) {
line = bufferedReader.readLine();
text.append(line);
}
bufferedReader.close();
fileReader.close();
return text.toString();
}
//统计文件中字符出现的比例并输出。
private static void CountOfChar() throws IOException {
String filename = "Object.txt";
System.out.printf("即将读取%s中的字符,并统计字母百分比。\n", filename);
String text = RandomNumber.readFile(filename);
int[] cnt = new int[26];
for (int i = 0; i < text.length(); i++) {
if (text.charAt(i) <= 'z' && text.charAt(i) >= 'a') {
int index = text.charAt(i) - 'a';
cnt[index]++;
} else if (text.charAt(i) <= 'Z' && text.charAt(i) >= 'A') {
int index = text.charAt(i) - 'A';
cnt[index]++;
}
}
int sum = 0;
for (int i = 0; i < 26; i++) {
sum += cnt[i];
}
for (int i = 0; i < 26; i++) {
System.out.printf("%c出现的次数占比为:%.3f%%\n", 'a' + i, (double) cnt[i] / (double) sum * 100);
}
}
public static void main(String args[]) throws IOException {
RandomNumber.CreateRandomNumber();
RandomNumber.CreateRandomChar();
RandomNumber.CountOfChar();
}
}
WriteReadFile
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class WriteReadFile {
//写入文件
public static void WriteFile(ArrayList<String[]> data, String path) throws FileNotFoundException {
PrintWriter out = new PrintWriter(path);
for (String[] each : data) {
StringBuilder line = new StringBuilder("");
for (String s : each) line.append(s).append(" ");
out.println(line);
}
out.close();
System.out.printf("%d个学生数据已经写入文件!", data.size());
}
//读入文件
public static ArrayList<String[]> ReadFile() throws FileNotFoundException {
ArrayList<String[]> data = new ArrayList<>();
Scanner in = new Scanner(new File("myfile.txt"));
int number = 1;
while (in.hasNextLine()) {
String line = in.nextLine();
//System.out.println("第" + (number++) + "行的内容" + ": " + line);
Scanner linescanner = new Scanner(line);//对每一行的内容建立一个扫描器
linescanner.useDelimiter(" ");//使用空格作为分隔符
String name = linescanner.next();
String math = linescanner.next();
String java = linescanner.next();
String ds = linescanner.next();
String avg = linescanner.next();
String total = linescanner.next();
//System.out.println("name=" + name + " math=" + math + " java=" + java + " ds=" + ds + " avg" + avg + " total=" + total);
String[] information = new String[]{name, math, java, ds, avg, total};
data.add(information);
}
in.close();
System.out.println("文件已经读入完毕!");
return data;
}
public static void main(String[] args) throws FileNotFoundException {
CreateStudent(10000);
ModifyInformation();
first_10();
findStudent("张三1");
}
public static void ModifyInformation() throws FileNotFoundException {
ArrayList<String[]> student = ReadFile();
ArrayList<String[]> data = new ArrayList<>();
for (String[] line : student) {
int sum = 0;
sum += Integer.parseInt(line[1]);
sum += Integer.parseInt(line[2]);
sum += Integer.parseInt(line[3]);
int average = sum / 3;
String[] one_student = new String[]{line[0], line[1], line[2], line[3], Integer.toString(average), Integer.toString(sum)};
data.add(one_student);
}
System.out.println("平均分和总分已经修改完毕!");
WriteFile(data, "myfile.txt");
System.out.println("\n--------------------------------------------\n");
}
//生成n个学生信息并写入文件
public static void CreateStudent(int n) throws FileNotFoundException {
ArrayList<String[]> students = new ArrayList<>();
for (int i = 0; i < n; i++) {
students.add(getStudent(i));
}
System.out.println("已经生成" + n + "个学生信息!");
WriteFile(students, "myfile.txt");
System.out.println("\n--------------------------------------------\n");
}
//生成单个学生信息
public static String[] getStudent(int i) {
String name = "张三" + i;
int math = (int) (Math.random() * 101), java = (int) (Math.random() * 101), ds = (int) (Math.random() * 101);
String[] student = new String[]{name, Integer.toString(math), Integer.toString(java), Integer.toString(ds), "0", "0"};
return student;
}
//找出平均成绩前10名的学生,写入“first_10.txt”文件。
public static void first_10() throws FileNotFoundException {
ArrayList<String[]> student = ReadFile();
int[] score = new int[10000];
for (int i = 0; i < student.size(); i++)
score[i] = Integer.parseInt(student.get(i)[5]);
int[] first10_index = new int[10];
for (int i = 0; i < 10; i++) {
int max = score[0];
for (int j = 0; j < 10000; j++) {
if (score[j] > max) {
max = score[j];
first10_index[i] = j;
}
}
score[first10_index[i]] = 0;
}
ArrayList<String[]> first10_information = new ArrayList<>();
for (int i = 0; i < 10; i++)
first10_information.add(student.get(first10_index[i]));
WriteFile(first10_information, "first_10.txt");
System.out.printf("已经把前10的学生信息录入《first_10.txt》!");
System.out.println("\n--------------------------------------------\n");
}
public static String[] findStudent(String name) throws FileNotFoundException {
ArrayList<String[]> student = ReadFile();
ArrayList<String> ans = new ArrayList<>();
for (String[] strings : student) {
if (strings[0].equals(name)) {
StringBuilder information = new StringBuilder("");
for (String each : strings)
information.append(each).append(" ");
ans.add(information.toString());
}
}
System.out.printf("已经找到与%s同名的学生%d个", name, ans.size());
System.out.println("\n--------------------------------------------\n");
return ans.toArray(new String[ans.size()]);
}
}
KnightTour
import javax.swing.plaf.synth.SynthOptionPaneUI;
import java.util.ArrayList;
import java.util.Scanner;
public class KnightTour {
static int MAX = 8;
static int[][] board = new int[MAX][MAX];
static int director[][] = {{1, 2}, {-1, 2}, {-2, 1}, {-2, -1}, {-1, -2}, {1, -2}, {2, -1}, {2, 1}};
static int flag = 0;
static void input()
{
Scanner scanner = new Scanner(System.in);
MAX = scanner.nextInt();
}
static void dfs(int step, int x, int y) {
if (step == MAX * MAX) {
flag = -1;
print();
return;
}
for (int i = 0; i < 8; i++)
if (x + director[i][0] >= 0 && x + director[i][0] < MAX &&
y + director[i][1] >= 0 && y + director[i][1] < MAX && board[x + director[i][0]][y + director[i][1]] == 0) {
board[x + director[i][0]][y + director[i][1]] = step + 1;
dfs(step + 1, x + director[i][0], y + director[i][1]);
if (flag != 0)
return;
board[x + director[i][0]][y + director[i][1]] = 0;
}
}
static void dfs_all(int step, int x, int y) {
int i;
if (step == MAX * MAX) {
flag++;
print();
return;
}
for (i = 0; i < 8; i++)
if (x + director[i][0] >= 0 && x + director[i][0] < MAX &&
y + director[i][1] >= 0 && y + director[i][1] < MAX && board[x + director[i][0]][y + director[i][1]] == 0) {
board[x + director[i][0]][y + director[i][1]] = step + 1;
dfs_all(step + 1, x + director[i][0], y + director[i][1]);
board[x + director[i][0]][y + director[i][1]] = 0;
}
}
public static void main(String[] args) {
input();
init();
dfs(1, 0, 0);
init();
dfs_all(1, 0, 0);
}
public static void print() {
if(flag==-1)
System.out.println("单次巡游结果:--------------------------------------------");
else
System.out.printf("第%d次巡游结果:--------------------------------------------\n",flag);
int i, j, n = MAX;
int[] x = new int[MAX * MAX + 1];
int[] y = new int[MAX * MAX + 1];
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
x[board[i][j]] = i;
y[board[i][j]] = j;
}
}
for (i = 1; i <= MAX * MAX; i++) {
System.out.printf("(%d,%d)", x[i], y[i]);
if (i != MAX * MAX)
System.out.print(" ---> ");
if (i % MAX == 0)
System.out.println();
}
}
public static void init() {
flag = 0;
int i, j, n = MAX;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
board[i][j] = 0;
}
}
board[0][0] = 1;
}
}
Main
public class Main {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}