- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Java实现的按照顺时针或逆时针方向输出一个数字矩阵功能示例由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文实例讲述了java实现的按照顺时针或逆时针方向输出一个数字矩阵功能。分享给大家供大家参考,具体如下:
题目:按照指定的长宽和输出方向,从外向内打印一个从 1 开始的数字矩阵,矩阵的开始位置在左上角。如下图 。
代码及注释如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
public
class
numbermatrix {
public
static
void
main(string[] args) {
int
width =
25
;
int
height =
12
;
boolean
clockwise =
false
;
system.out.println(
"我测试结果:"
);
outputmatrix(width, height, clockwise);
}
/**
* 按照指定的长宽和输出方向,从外向内打印一个从 1 开始的数字矩阵,矩阵的开始位置在左上角。
*
* @param width 矩阵宽度
* @param height 矩阵高度
* @param clockwise 是否是顺时针方向
*/
private
static
void
outputmatrix(
int
width,
int
height,
boolean
clockwise) {
// 首先判断最大数字的位数,以决定输出如何对齐
int
numlength = (
int
) math.log10(width * height) +
1
;
// 决定输出的格式(最大位数 + 1个空格)
string format =
"%"
+ (numlength +
1
) +
"d"
;
// 定义要输出的二维数组,注意维度是从高到低的
// 此时 matrix 中所有元素的值都是 0
int
[][] matrix =
new
int
[height][width];
// 定义一个位置指针和一个计数器,位置指针进行移动,而计数器负责递增,递增后的数字
// 被填充进矩阵,当 width * height 个数字填充完毕,这个矩阵就完成了。
// 注意这里位置指针的第一个元素对应 matrix 的第一个维度 y,第二个元素对应第二个维度 x。
int
[] pointer = {
0
,
0
};
int
counter =
1
;
// 定义当前移动的方向:1、2、3、4 分别表示上、右、下、左。
// 顺时针的起始方向为右,逆时针的起始方向为下。
int
direction = clockwise ?
2
:
3
;
// 开始循环填充,每个填充分为三步
for
(
int
i =
1
, max = width * height; i <= max; i++) {
// 1. 填充内容
int
y = pointer[
0
];
int
x = pointer[
1
];
matrix[y][x] = counter;
// 2. 计数器自增
counter +=
1
;
// 3. 移动到下一个位置,因为这地方比较复杂,所以开个方法实现
direction = move(matrix, width, height, pointer, direction, clockwise);
}
// 矩阵填充完毕,按照正常的方式循环输出即可
for
(
int
y =
0
; y < height; y++) {
for
(
int
x =
0
; x < width; x++) {
system.out.printf(format, matrix[y][x]);
}
system.out.println();
// 完成一行后输出换行
}
}
/**
* 在矩阵中移动
*
* @param matrix 矩阵,用于判断前进方向的下一个位置是否已经填充了数字,如果是则转向
* @param width 矩阵的宽
* @param height 矩阵的高
* @param pointer 指针的当前位置。调用本方法后里面的值会改变,除非方法返回 0
* @param direction 指针当前移动的方向
* @param clockwise 是否是要按顺时针方向转向
*
* @return 移动后的新方向(与原来的方向可能相同也可能不同)。如果无法再继续移动,则返回 0
*/
private
static
int
move(
int
[][] matrix,
int
width,
int
height,
int
[] pointer,
int
direction,
boolean
clockwise) {
// 先尝试按照原来的方向移动到 newpointer
int
[] newpointer = movedirectly(pointer, direction);
// 检查 newpointer 是否合法,如果合法则将其赋值给 pointer 并保持原来的方向,方法完成
if
(isvalid(newpointer, matrix, width, height)) {
system.arraycopy(newpointer,
0
, pointer,
0
,
2
);
return
direction;
}
// 进行转向,重新从 pointer 朝新的方向移动
direction = turn(direction, clockwise);
newpointer = movedirectly(pointer, direction);
// 检查 newpointer 是否合法(同前面一样)
if
(isvalid(newpointer, matrix, width, height)) {
system.arraycopy(newpointer,
0
, pointer,
0
,
2
);
return
direction;
}
// 既无法前进也无法转向,那么无法继续移动。
return
0
;
}
// 判断矩阵中指定的位置是否可以填充
private
static
boolean
isvalid(
int
[] newpointer,
int
[][] matrix,
int
width,
int
height) {
// 位置不能超出矩阵范围
if
(newpointer[
0
] >= height
|| newpointer[
0
] <
0
|| newpointer[
1
] >= width
|| newpointer[
1
] <
0
) {
return
false
;
}
// 位置的内容应该为空
if
(matrix[newpointer[
0
]][newpointer[
1
]] !=
0
) {
return
false
;
}
return
true
;
}
// 转向。根据我们对 direction 的定义,顺时针就是 +1,逆时针就是 -1
private
static
int
turn(
int
direction,
boolean
clockwise) {
int
newdirection = clockwise ? direction +
1
: direction -
1
;
if
(newdirection >
4
) {
newdirection =
1
;
}
else
if
(newdirection <
1
) {
newdirection =
4
;
}
return
newdirection;
}
/**
* 朝指定的方向移动,并返回新的位置
*
* @param pointer 当前位置
* @param direction 方向
*
* @return 新的位置
*/
private
static
int
[] movedirectly(
int
[] pointer,
int
direction) {
int
y = pointer[
0
];
int
x = pointer[
1
];
switch
(direction) {
case
1
:
return
new
int
[]{y -
1
, x};
case
2
:
return
new
int
[]{y, x +
1
};
case
3
:
return
new
int
[]{y +
1
, x};
case
4
:
return
new
int
[]{y, x -
1
};
}
throw
new
illegalargumentexception(
"方向不正确: "
+ direction);
}
}
|
运行结果:
希望本文所述对大家java程序设计有所帮助.
原文链接:http://blog.csdn.net/YidingHe/article/details/49924735 。
最后此篇关于Java实现的按照顺时针或逆时针方向输出一个数字矩阵功能示例的文章就讲到这里了,如果你想了解更多关于Java实现的按照顺时针或逆时针方向输出一个数字矩阵功能示例的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在使用 OUTFILE 命令,但由于权限问题和安全风险,我想将 shell 的输出转储到文件中,但出现了一些错误。我试过的 #This is a simple shell to connect t
我刚刚开始学习 Java,我想克服在尝试为这个“问题”创建 Java 程序时出现的障碍。这是我必须创建一个程序来解决的问题: Tandy 喜欢分发糖果,但只有 n 颗糖果。对于她给第 i 个糖果的人,
你好,我想知道我是否可以得到一些帮助来解决我在 C++ 中打印出 vector 内容的问题 我试图以特定顺序在一个或两个函数调用中输出一个类的所有变量。但是我在遍历 vector 时收到一个奇怪的错误
我正在将 intellij (2019.1.1) 用于 java gradle (5.4.1) 项目,并使用 lombok (1.18.6) 来自动生成代码。 Intellij 将生成的源放在 out
编辑:在与 guest271314 交流后,我意识到问题的措辞(在我的问题正文中)可能具有误导性。我保留了旧版本并更好地改写了新版本 背景: 从远程服务器获取 JSON 时,响应 header 包含一
我的问题可能有点令人困惑。我遇到的问题是我正在使用来自 Java 的 StoredProcedureCall 调用过程,例如: StoredProcedureCall call = new Store
在我使用的一些IDL中,我注意到在方法中标记返回值有2个约定-[in, out]和[out, retval]。 当存在多个返回值时,似乎使用了[in, out],例如: HRESULT MyMetho
当我查看 gar -h 的帮助输出时,它告诉我: [...] gar: supported targets: elf64-x86-64 elf32-i386 a.out-i386-linux [...
我想循环遍历一个列表,并以 HTML 格式打印其中的一部分,以代码格式打印其中的一部分。所以更准确地说:我想产生与这相同的输出 1 is a great number 2 is a great
我有下面的tekton管道,并尝试在Google Cloud上运行。集群角色绑定。集群角色。该服务帐户具有以下权限。。例外。不确定需要为服务帐户设置什么权限。
当尝试从 make 过滤非常长的输出以获取特定警告或错误消息时,第一个想法是这样的: $ make | grep -i 'warning: someone set up us the bomb' 然而
我正在创建一个抽象工具类,该类对另一组外部类(不受我控制)进行操作。外部类在某些接口(interface)点概念上相似,但访问它们相似属性的语法不同。它们还具有不同的语法来应用工具操作的结果。我创建了
这个问题已经有答案了: What do numbers starting with 0 mean in python? (9 个回答) 已关闭 7 年前。 在我的代码中使用按位与运算符 (&) 时,我
我写了这段代码来解析输入文件中的行输入格式:电影 ID 可以有多个条目,所以我们应该计算平均值输出:**没有重复(这是问题所在) import re f = open("ratings2.txt",
我需要处理超过 1000 万个光谱数据集。数据结构如下:大约有 1000 个 .fits(.fits 是某种数据存储格式)文件,每个文件包含大约 600-1000 个光谱,其中每个光谱中有大约 450
我编写了一个简单的 C 程序,它读取一个文件并生成一个包含每个单词及其出现频率的表格。 该程序有效,我已经能够在 Linux 上运行的终端中获得显示的输出,但是,我不确定如何获得生成的显示以生成包含词
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
1.普通的输出: print(str)#str是任意一个字符串,数字··· 2.格式化输出: ?
我无法让 logstash 正常工作。 Basic logstash Example作品。但后来我与 Advanced Pipeline Example 作斗争.也许这也可能是 Elasticsear
这是我想要做的: 我想让用户给我的程序一些声音数据(通过麦克风输入),然后保持 250 毫秒,然后通过扬声器输出。 我已经使用 Java Sound API 做到了这一点。问题是它有点慢。从发出声音到
我是一名优秀的程序员,十分优秀!