黑马程序员技术交流社区
标题:
自己写的一个杨辉三角
[打印本页]
作者:
黎健东
时间:
2012-8-12 16:25
标题:
自己写的一个杨辉三角
本帖最后由 黎健东 于 2012-8-12 16:27 编辑
package com.lee.graphic;
public class YangHuiTriangle {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int row = 10;
int colunm = row * 2;
int half_colunm = colunm / 2;
int [][]array = new int [row][colunm];
int index = 0;
for(int i = 0; i < row; i++){
//左边三角形
for(int j = 0; j < half_colunm - i - 1; j++){
array[i][j] = 0;
index++;
}
//为单独的1赋值
array[i][index++] = 1;
//从第2行起,规律为里边的任意一个数等于上一行的左上+右上
if(i != 0){
//内部三角形
for(int k = 0; k < i * 2; k++){
if(k % 2 == 0){
array[i][index] = 0;
}else{
array[i][index] = array[i - 1][index - 1] + array[i - 1][index + 1];
}
index++;
}
//计算剩余的行
int lastColunm = row - index;
for(int j = 0; j < lastColunm; j++){
array[i][index] = 0;
index++;
}
}
//每一行重置指针index
index = 0;
}
//格式输出
for(int i = 0; i < row; i++){
for(int j = 0; j < colunm; j++){
if(array[i][j] == 0){
System.out.printf("%-3s","");
}else{
System.out.printf("%-3s",array[i][j]);
}
}
System.out.println();
}
}
}
复制代码
自己写的一个杨辉三角
还有需要改进的地方,欢迎拍砖,求加分
yanghuisanjiao.jpg
(24.03 KB, 下载次数: 16)
下载附件
2012-8-12 16:27 上传
作者:
黎健东
时间:
2012-8-13 16:43
花心思不看网上的自己写的,求加1分技术分{:soso_e154:}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2