* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int row=5//这里控制输出的行列 请保证行列相等,这里就不做判断了
int[][] a=new int[row][row]
a[0][0]=1
int n=2,zero=0,change=1
int value=2
for(int i=1i<rowi++){
for(int j=0j<(2*n-1)j++){
if(i%2!=0){
if(j<(2*n-2)/2){
a[zero++][change]=value++
continue
}
if(j==(2*n-2)/2){
a[j][j]=value++
}
if(j>(2*n-2)/2){
a[change][--zero]=value++
}
}else{
if(j<(2*n-2)/2){
a[change][zero++]=value++
continue
}
if(j==(2*n-2)/2){
a[j][j]=value++
}
if(j>(2*n-2)/2){
a[--zero][change]=value++
}
}
}
n++
change++
zero=0
}
String out=""
for(int i=0i<rowi++){
for(int j=0j<rowj++){
out=""+a[i][j]
if(out.length()==1)
out="0"+out
System.out.print(out+" ")
}
System.out.println()
}
}}
运行测试通过
写个函数,也是来自于tp官方function list_to_tree($list, $pk='id', $pid = 'pid', $child = '_child', $root = 0) {
// 创建Tree
$tree = array()
if(is_array($list)) {
// 创建基于主键的数组引用
$refer = array()
foreach ($list as $key =>$data) {
$refer[$data[$pk]] =&$list[$key]
}
foreach ($list as $key =>$data) {
// 判断是否存在parent
$parentId = $data[$pid]
if ($root == $parentId) {
$tree[] =&$list[$key]
}else{
if (isset($refer[$parentId])) {
$parent =&$refer[$parentId]
$parent[$child][] =&$list[$key]
}
}
}
}
return $tree
}
然后定义一维数组为$list,然后 print_r(list_to_tree($list,"id","parentsid","subnav"))
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)