1. 程式人生 > >[Bash]函式與分形-遞迴樹

[Bash]函式與分形-遞迴樹

 

____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
____________________________________________________________________________________________________
_________________________________1_______________________________1__________________________________
__________________________________1_____________________________1___________________________________
___________________________________1___________________________1____________________________________
____________________________________1_________________________1_____________________________________
_____________________________________1_______________________1______________________________________
______________________________________1_____________________1_______________________________________
_______________________________________1___________________1________________________________________
________________________________________1_________________1_________________________________________
_________________________________________1_______________1__________________________________________
__________________________________________1_____________1___________________________________________
___________________________________________1___________1____________________________________________
____________________________________________1_________1_____________________________________________
_____________________________________________1_______1______________________________________________
______________________________________________1_____1_______________________________________________
_______________________________________________1___1________________________________________________
________________________________________________1_1_________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________
_________________________________________________1__________________________________________________

Functions and Fractals - Recursive Trees - Bash!

 

#!/bin/bash
#https://www.hackerrank.com/challenges/fractal-trees-all/problem?h_r=next-challenge&h_v=zen
read N

COL=100
ROW=63

total=$((ROW*COL))
for((i=0;i<total;i++))
do
    arr[i]="_"
done

#echo "$total"
#echo "${#arr[*]}"
#echo "${#arr[@]}"
#echo "${arr[@]}"

if [ "$N" -ne "0" ]
then

row=62
t=16

for((i=0;i<COL;i++))
do
    line[i]="_"
done
line[49]="1"

#echo "${line[*]}"
#echo "${#line[*]}"

for((n=1;n<=N;n++))
do
    for((i=0;i<t;i++))
    do
        init=$((COL*row))
        for((j=0;j<COL;j++))
        do
            arr[init]=${line[j]}
            let init++
        done
        let row--
    done

    for((i=0;i<t;i++))
    do
        odd=1
        for((k=0;k<COL;k++))
        do
            if [[ $i -eq 0 && "${line[k]}" == "1" ]]
            then
                line[k]="_"
                line[k-1]="1"
                line[k+1]="1"
                let k++
            elif [[ $i -ne 0 && "${line[k]}" == "1"  ]]
            then
                if [[ $odd -eq 1 ]]
                then
                    line[k]="_"
                    line[k-1]="1"
                    odd=0
                else
                    line[k]="_"
                    line[k+1]="1"
                    let k++
                    odd=1
                fi
            fi

        done
        init=$((COL*row))
        for((j=0;j<COL;j++))
        do
            arr[init]=${line[j]}
            let init++
        done
        let row--
    done
    t=$((t/2))
done
fi

k=0
for((i=0;i<ROW;i++))
do
    for((j=0;j<COL;j++))
    do
        echo -e "${arr[k]}\c"
        let k++
    done
    echo
done

輸出的樣例