打印数字的同心平方

编程入门 行业动态 更新时间:2024-10-27 05:24:45
本文介绍了打印数字的同心平方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在练习模式,直到达到这种模式为止.它表示以最大层为 NxN 和最小层为 1x1

I have been practicing patterns until I reached this pattern. It says print concentric squares of numbers with max layer as NxN and Min layer 1x1

For instance, for input N=3 the desired o/p should be 33333 32223 32123 32223 33333

我尝试过

for(int i=1;i<=2*N-1;i++) { for(int j=1;j<=2*N-1;j++) { if(i==1 || i==N) --for max n min layer System.out.print(N); else if(j!=1 || j!=N) --for col not as max n min and rows between max n min System.out.print(N-1); else System.out.print(N); }}

我知道这是错误的,但我希望按照上述示例的逐步方法以常规方式解决.

I know that its wrong but I want it to be solved in a conventional way as above sample meaning step by step.

推荐答案

尝试以下代码,您可以根据需要更改以下代码

Try this below code and you can change the below code according to your requirement

Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int size = 2*n-1; int start=0; int end=size-1; int [][]a = new int[size][size]; while(n!=0) { for (int i=start;i<=end;i++) { for (int j=start;j<=end;j++) { if((i==start)||(j==start)||(i==end)||(j==end)) a[i][j] = n; } } start++; end--; n--; } for (int i=0;i<size;i++) { for (int j=0;j<size;j++) { System.out.print(a[i][j]+" "); } System.out.println(); }

更多推荐

打印数字的同心平方

本文发布于:2023-08-07 06:58:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1317621.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数字

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!