当前位置:网站首页>再做螺旋矩阵AcWing753 756【写法妙】
再做螺旋矩阵AcWing753 756【写法妙】
2022-08-05 17:53:00 【kkzz1x】
756. 蛇形矩阵
输入样例:
3 3
输出样例:
1 2 3
8 9 4
7 6 5
在题解区看到一个非常妙非常好理解的写法!!!故此记录。
对于矩阵填数的问题,都可以用这种方式解决了。
import java.util.Scanner;
/** * @program: AcWing * @description: * @author: zjx * @create: 2022-08-01 12:05 **/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int[][] g=new int[105][105];
int left=0,right=m-1,top=0,bottom=n-1;
int cnt=1;
while(left<=right&&top<=bottom){
//向右
for(int i=left;i<=right;i++){
g[top][i]=cnt++;
}
//向下
for(int i=top+1;i<=bottom;i++){
g[i][right]=cnt++;
}
//向左
for(int i=right-1;i>=left && top < bottom;i--){
// 注意条件,否则往回写会覆盖…… 比如输入 1 4,只要执行向右就行,然而不加这个语句 top==bottom就会覆盖之前写的导致错误
g[bottom][i]=cnt++;
}
//向上
for(int i=bottom-1;i>=top+1 && left<right;i--){
// 注意条件
g[i][left]=cnt++;
}
left++;right--;top++;bottom--;
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
System.out.print(g[i][j]+" ");
}
System.out.println();
}
}
}
AcWing753. 平方矩阵 I
输入样例:
1
2
3
4
5
0
输出样例:
1
1 1
1 1
1 1 1
1 2 1
1 1 1
1 1 1 1
1 2 2 1
1 2 2 1
1 1 1 1
1 1 1 1 1
1 2 2 2 1
1 2 3 2 1
1 2 2 2 1
1 1 1 1 1
如果用之前的方法,虽然也比较简单,但是只能用于n×n矩阵(无法套用在n×m矩阵中)
import java.util.Scanner;
/** * @program: AcWing * @description: * @author: zjx * @create: 2022-08-01 12:05 **/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true){
int n=sc.nextInt();
if(n==0) break;
int[][] g=new int[105][105];
int si=0,sj=0;
int cnt=n-1;
int s=1;
int i,j;
// 向右
while(cnt>0) {
for (j = sj; j < sj + cnt; j++) {
g[si][j] = s;
}
//向下
for (i = si; i < si + cnt; i++) {
g[i][j] = s;
}
//向左
for (j = sj + cnt; j > sj; j--) {
g[i][j] = s;
}
//向上
for (i = si + cnt; i > si; i--) {
g[i][j] = s;
}
s++;
si++;
sj++;
cnt -= 2;
}
if(n%2==1) g[si][sj]=s;
for(i=0;i<n;i++){
for(j=0;j<n;j++){
System.out.print(g[i][j]+" ");
}
System.out.println();
}
System.out.println();
}
}
}
借鉴了756 蛇形矩阵问题后:
import java.util.Scanner;
/** * @program: AcWing * @description: * @author: zjx * @create: 2022-08-01 12:05 **/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int[][] g=new int[105][105];
int left=0,right=n-1,top=0,bottom=n-1;
int cnt=1;
while(left<=right&&top<=bottom){
//向右
for(int i=left;i<=right;i++){
g[top][i]=cnt;
}
//向下
for(int i=top+1;i<=bottom;i++){
g[i][right]=cnt;
}
//向左
for(int i=right-1;i>=left && top < bottom;i--){
// 注意条件,否则往回写会覆盖…… 比如输入 1 4,只要执行向右就行,然而不加这个语句 top==bottom就会覆盖之前写的导致错误
g[bottom][i]=cnt;
}
//向上
for(int i=bottom-1;i>=top+1 && left<right;i--){
// 注意条件
g[i][left]=cnt;
}
left++;right--;top++;bottom--;
cnt++;
}
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
System.out.print(g[i][j]+" ");
}
System.out.println();
}
}
}
边栏推荐
- rk3399 休眠唤醒standby调试指南
- 宝塔实测-电商ERP进销存系统源码
- ORACLE ORA-01653: how to deal with the error of unable to extend table
- Photoshop抠图-边缘调整
- img幽灵空白的解决方法
- 【边缘计算概念】MEC(Mobile Edge Computing边缘计算技术)
- 4、常用样式
- PHP需要掌握的东西
- Which platform is the best for futures account opening? I want this kind of formal and safe company
- rk3399 内核调试
猜你喜欢
随机推荐
【OAuth2】十四、OAuth2授权端点配置类OAuth2AuthorizationEndpointConfigurer和OAuth2AuthorizationEndpointFilter
uniapp中用canvas实现小球碰撞的小动画
Kettle需求场景复现
Moyck 的 App 库
DRM架构下如何点亮一块屏
【OAuth2】十五、客户端认证流程-自定义授权页面和客户端认证
Harbor 2.x API整理:获取指定project下的helm charts列表
生成扩散模型漫谈:DDIM = 高观点DDPM
WPF 截图控件之绘制箭头(五)「仿微信」
力扣623.在二叉树中增加一行 dfs
【已解决】pnpm(Run “pnpm setup“ to create it automatically, or set the global-bin-dir setting, or the PNP
[Cloud Native Kubernetes Series] KubeSphere Container Scheduling Platform
一部电影干出57.75亿,这个北京爷们儿要去IPO敲钟了
每日一题(1)—— 最大连号
EN 12259-5固定消防系统水流探测器—CE认证
通过软链接mklink共享node_modules
【623. 在二叉树中增加一行】
Shell自动化编程初识
做个男人,做个成熟的男人
备份阿里云数据库到本地数据库









