试题说明
当前目录下input.txt文件中有数字矩阵,每行为一条记录,行数4行,每条记录有4个字段,字段类型为正整数,字段间以逗号分隔,忽略空白字符(即可以有空格等),如:
12, 2, 11, 8
1, 13, 3, 9
4, 10, 16, 7
5, 15, 14, 6
输出要求
1、程序读入input.txt中的数据,经过下列处理后,输出到当前目录下output.txt文件中
2、按从小到大逆时针方向依次写在4*4矩阵中。结果如下:
1, 12, 11, 10
2, 13, 16, 9
3, 14, 15, 8
4, 5, 6, 7
关于异常
如果输入文件中的格式不对(如:某记录的字段数量不对,某字段值为非整型字符等),在输出文件中打印“INPUT ERROR”。注意,此时输出文件中只能有这个错误信息(不包含双引号),无其他信息。
如果输入文件不存在,可以抛出IOException。
但不允许抛出空指针异常。涉及到的考点:
文件读写
常用排序
字符串处理(可以使用正则表达式)
当前目录下input.txt文件中有数字矩阵,每行为一条记录,行数4行,每条记录有4个字段,字段类型为正整数,字段间以逗号分隔,忽略空白字符(即可以有空格等),如:
12, 2, 11, 8
1, 13, 3, 9
4, 10, 16, 7
5, 15, 14, 6
输出要求
1、程序读入input.txt中的数据,经过下列处理后,输出到当前目录下output.txt文件中
2、按从小到大逆时针方向依次写在4*4矩阵中。结果如下:
1, 12, 11, 10
2, 13, 16, 9
3, 14, 15, 8
4, 5, 6, 7
关于异常
如果输入文件中的格式不对(如:某记录的字段数量不对,某字段值为非整型字符等),在输出文件中打印“INPUT ERROR”。注意,此时输出文件中只能有这个错误信息(不包含双引号),无其他信息。
如果输入文件不存在,可以抛出IOException。
但不允许抛出空指针异常。涉及到的考点:
文件读写
常用排序
字符串处理(可以使用正则表达式)
import java.io.*;
import java.util.*;public class Test
{
public static final int UP = 0;
public static final int DOWN = 1;
public static final int LEFT = 2;
public static final int RIGHT = 3;
public static final int ROW_COUNT = 4;
public static final int COL_COUNT = 4;
public static void main(String[] args) throws IOException
{
BufferedReader in = new BufferedReader(new FileReader("input.txt"));
PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
//首先读取文本中的所有数据,并把它们存在一个数组中
int[] numbers = new int[ROW_COUNT * COL_COUNT];
for (int i = 0; i < ROW_COUNT; i++)
{
String str = in.readLine();
String[] strs = str.split(",");
for (int j = 0; j < COL_COUNT; j++)
{
try
{
numbers[i * COL_COUNT + j] = Integer.parseInt(strs[j].trim());
}
catch (NumberFormatException e)
{
out.println("INPUT ERROR");
in.close();
out.close();
return;
}
catch (ArrayIndexOutOfBoundsException e)
{
out.println("INPUT ERROR");
in.close();
out.close();
}
}
}
Arrays.sort(numbers); //对数据进行排序
//新建一个二维数组,起始方向是向下
int[][] outputNumbers = new int[ROW_COUNT][COL_COUNT];
for (int i = 0; i < ROW_COUNT; i++)
{
for (int j = 0; j < COL_COUNT; j++)
{
outputNumbers[i][j] = Integer.MAX_VALUE;
}
}
int forward = DOWN;
//根据顺序对二维数组进行赋值
int row = 0;
int col = 0;
for (int i = 0; i < numbers.length; i++)
{
outputNumbers[row][col] = numbers[i];
switch (forward)
{
case UP:
row--;
if (row < 0 || outputNumbers[row][col] != Integer.MAX_VALUE)
{
row++;
col--;
forward = LEFT;
}
break;
case DOWN:
row++;
if (row >= ROW_COUNT || outputNumbers[row][col] != Integer.MAX_VALUE)
{
row--;
col++;
forward = RIGHT;
}
break;
case LEFT:
col--;
if (col < 0 || outputNumbers[row][col] != Integer.MAX_VALUE)
{
col++;
row++;
forward = DOWN;
}
break;
case RIGHT:
col++;
if (col >= COL_COUNT || outputNumbers[row][col] != Integer.MAX_VALUE)
{
col--;
row--;
forward = UP;
}
break;
}
}
//将数据打印到文件中
for (int i = 0; i < ROW_COUNT; i++)
{
for (int j = 0; j < COL_COUNT; j++)
{
out.print(outputNumbers[i][j]);
if (j != COL_COUNT - 1)
{
out.print(", ");
}
}
out.println();
}
in.close();
out.close();
}
}
//矩阵的方向换一下。原理一样
public class N2N { // 新建二维数组,保存矩阵的值
int[][] array = null; public static void main(String[] args) {
N2N n2n = new N2N();
n2n.init(9);
n2n.show();
} public void show() {
// 输出矩阵的值
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array.length; j++) {
System.out.print(array[i][j] + "\t");
}
System.out.println();
}
} public void init(int n) {
array = new int[n][n];
result(n, n - 1, n, 0, (n - 1) * 4);
} /**
* 求n*n矩阵的值
*
* @param n
* 矩阵数,递减,每次-2,到0或者1为止
* @param index
* 当前矩阵的最大索引
* @param m
* 矩阵的开始数
* @param time
* 第几个矩阵,从0开始
* @param max
* 当前矩阵的最大值
*/
public void result(int n, int index, int m, int time, int max) {
// 表示矩阵每一边2个顶点之间的间隔点数,矩阵公4个边,4个顶点
int count = n - 1; // 个数(总共有多少个点数)
int num = count * 4; // 本矩阵的开始值(矩阵的第一个顶点的值)
int one = max - num + 1;
// 本矩阵的开始值(矩阵的第一个顶点的值)
int two = one + count; // 本矩阵的结束值(矩阵的第四个,最后一个顶点的值)
// 或者four=max-count-1
int four = one + count * 3; // 本矩阵Y轴的开始索引
int y1 = index + 1 - n;
// 本矩阵Y轴的最大索引
int y2 = index;
// 本矩阵X轴的开始索引
int x1 = index + 1 - n;
// 本矩阵X轴的最大索引
int x2 = index; // +time或者-time主要是使顶点处的值加索引为0,4个顶点只能加0,
// 因为每次递归下一个矩阵,第一个顶点的索引就会从0开始依次加1 for (int i = y1; i <= y2; i++) {
for (int j = x1; j <= x2; j++) {
// 给矩阵X轴的最上边赋值,从第1个顶点开始依次加1,到第2个顶点为止
if (i == y1) {
array[i][j] = one + (j - time);
}
// 给矩阵Y轴的最右边赋值,从第2个顶点开始依次加1,到第3个顶点为止
if (j == x2) {
array[i][j] = two + (i - time);
}
// 给矩阵X轴的最下边赋值,从第4个顶点开始依次减1,到第3个顶点为止
if (i == y2) {
array[i][j] = four - j + time;
}
// 给矩阵Y轴的最左边赋值,从最大值处(第1个顶点的下方)开始依次减1,到第4个顶点为止
if (j == x1) {
// 排除第一个顶点,因为第1个顶点与最大值之间相隔>1
if (i != y1) {
array[i][j] = max - i + time + 1;
}
}
}
} // 下一个矩阵数
n = n - 2;
// 下一个矩阵的最大索引
index = index - 1;
// 下一矩阵的个数,点数
num = (n - 1) * 4;
// 下一个矩阵的最大值
max = max + num;
// 递归下一个矩阵,每一个矩阵之间隔2,偶数到0,奇数到1(最后一个最大)
if (n != 0 && n != 1 && n > 0) {
result(n, index, m, time + 1, max);
}
// 给奇数最后一个赋值
if (n == 1) {
array[m / 2][m / 2] = m * m;
}
}
}