java金字塔等边三角形_用JAVA算法制作直角金字塔[关闭]

这将达到目的。您可以将数字5更改为5以外的其他数字。例如1,2,3,…,6,8

public static void main(String[] args) {

List list = new LinkedList();

for(int i = 5; i > 0; i-- ){

wrapWithNumber(list, i);

}

for (String string : editListToBeInTriangleShape(list)) {

System.out.println(string);

};

}

/**

* Wrap the number strings in the llist with a perticular number.

* @param list list of Strings

* @param ba number which need to wrapp the list with.

*/

private void wrapWithNumber(List list, final int ba) {

list.add(0, String.format("%d",ba));

for (int i = 1; i < list.size(); i++) {

String newformat = "%1$d " + list.get(i) + " %1$d";

list.remove(list.get(i));

list.add(i,String.format(newformat, ba));

}

String lastFormat = "%1$d";

for(int i = 0; i < 2 * list.size();i++){

lastFormat += " %1$d";

}

if(list.size() != 1) {

list.add(String.format(lastFormat, ba));

}

}

/**

* Arrage the Strings in the list in triangular manner.

* @param list list of Strings.

*/

private List editListToBeInTriangleShape(final List list) {

final List returnList = new LinkedList();

for (int i = list.size(); i > 0; i--) {

String s = list.get(list.size()-i);

int possition = list.size()*2 + s.length()/2;

returnList.add(String.format("%"+possition+"s", s));

}

return returnList;

}

排除此问题:

1

1 2 1

1 2 3 2 1

1 2 3 4 3 2 1

1 2 3 4 5 4 3 2 1

1 2 3 4 4 4 4 4 3 2 1

1 2 3 3 3 3 3 3 3 3 3 2 1

1 2 2 2 2 2 2 2 2 2 2 2 2 2 1

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值