「java塔层数」塔的层数怎么计算

博主:adminadmin 2023-01-12 21:03:09 337

今天给各位分享java塔层数的知识,其中也会对塔的层数怎么计算进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

在Java中实现数字金字塔

你好,代码如下,与你图中的一模一样:

public class Pyramid {

/*

* param h 金字塔高

* 返回 金字塔矩阵

*/

public static String[][] getPyramid(int h){

//根据图像可以观察只

//每一个数据都是有*和数字组成 而且规律是:

//如果数子长度为1,那么前面有两个*

//如果数子长度为2,那么前面有一个*

//根据高度来计算数组的宽度

int w = 1 + (h-1)*2 ;

//

String[][] array = new String[h][w] ;

for(int i = 0;ih;i++){

for(int j=0;jw;j++){

if(jh-i-1){

array[i][j] = "***";

}else if(jh){

if((h-j)9){

array[i][j] = "*" + (h-j) ;

}else{

array[i][j] = "**" + (h-j) ;

}

}else if(jh+i){

if((h-j)9){

array[i][j] = "*" + (j-h+2) ;

}else{

array[i][j] = "**" + (j-h+2) ;

}

}else{

array[i][j] = "";

}

}

}

return array ;

}

public static void print(String[][] array){

for(int i=0;iarray.length;i++){

for(int j=0;jarray[i].length;j++){

System.out.print(array[i][j]) ;

}

System.out.println();

}

}

public static void main(String[] args) {

print(getPyramid(12)) ;

}

}

如何用java实现二叉树特定的层数的值的个数?

public class Tree {

public Tree left;

public Tree right;

public int calc(int level) {

int result = 0;

if (level == 0)

result = 1;

else {

if (left != null)

result += left.calc(level - 1);

if (right != null)

result += right.calc(level - 1);

}

return result;

}

/**

* 计算二叉树特定层的节点数

*

* @param tree

* 二叉树

* @param level

* 层

* @return

*/

public static int calc(Tree tree, int level) {

return tree == null || level 0 ? 0 : tree.calc(level);

}

}

java程序设计 用for语句输出下列数字金字塔(层数由键盘输入): 1 131 13531

import java.util.Scanner;

public class jjj {

/**

* @param args

*/

public static void main(String[] args) {

int height=new Scanner(System.in).nextInt();//接受传进来的层数

int width=height*2-1;

for(int i=0;iheight;i++){

for(int j=i;jwidth/2;j++){

System.out.print(" ");

}

for(int k=0;ki*2+1;k++){

if(k=(i*2+1)/2){

System.out.print(k*2+1);

}else {

System.out.print(i*2+1-2*(k-i));

}

}

for(int j=i;jwidth/2;j++){

System.out.print(" ");

}

System.out.println();

}

}

}

谁能给我讲解一下这段java代码?

public class HanoiTower {

//次方法用于显示输出

static void moves(char i, char j) {

System.out.println("From " + i + " To " + j);

}

//汉诺塔递归函数n 表示层数,a,b,c表示塔盘,a是起始塔盘,c是目的塔盘,b是移动中用于暂时放置塔层的塔盘

//目的是借助b将a盘上的n层移动到c上,而且每层塔位于塔盘上时只能是上小下大 类似: -

// --

// ----

static void hanoi(int n, char a, char b, char c) {

//只有一层的话直接由a移动到c,无须借助b

if (n == 1) {

moves(a, c);

} else {

//如果不止一层,先把上面的n-1层借助c从a移动到b上,递归调用自身

hanoi(n - 1, a, c, b);

//把最底下最大的一层由a移动到c上

moves(a, c);

//此时b上有n-1层再借助a移动到c上,递归调用自身

hanoi(n - 1, b, a, c);

}

}

public static void main(String[] args) {

int n = 5;//假设有5层

//n = Integer.parseInt(args[0]);

hanoi(n, 'A', 'B', 'C');

//测试

//大一学C++讲函数用的就是这题目,老师叫板谁如果会就别来上课了,现在看看其实也没什么-_-!~...

}

}

java中怎样输入n,根据n的值,输出n层用字符"*"构成的"*"符塔

import java.util.Scanner;

public class a1 {

public static void main(String[] args) {

Scanner a=new Scanner(System.in);

int N=5;//定义行数的变量

boolean b=true;

do

{

try

{

System.out.println("请输入整数类型的数字:");

N=a.nextInt(); //获取输入行数

b=false;

}

catch(Exception ea)

{

a=new Scanner(System.in);

// N=a.nextInt(); //获取输入行数

}

}while(b);

int i,j,m;

for(i=0;iN;i++)//输出金字塔

{

for(m=0;mN-1-i;m++)

{

System.out.printf(" ");

}

for(j=0;j2*i+1;j++)

{

System.out.printf("*");

}

System.out.printf("\n");

}

}

}

java塔层数的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于塔的层数怎么计算、java塔层数的信息别忘了在本站进行查找喔。