美文网首页
shell实现永辉三角

shell实现永辉三角

作者: tmax | 来源:发表于2018-07-07 00:50 被阅读0次
#!/bin/bash
expr $1 + 1 &>/dev/null
flag=$?
if [ $flag -ne 0 -o $1 -lt 1 ]
then
        echo "参数错误!"
else
        for((i=0;i<$1;i++))
        do
                for((j=0;j<=i;j++))
                do
                        if [ $j -eq 0 ]
                        then
                                declare v_${i}_${j}=1
                        else
                                declare v_${i}_${j}=$[v_$[i-1]_$[j]+v_$[i-1]_$[j-1]]
                        fi
                        echo -n $[v_${i}_${j}]
                        echo -n "  "
                done
        echo ""
        done
fi

效果

ptmax@ubuntu:~/shell$ bash ./yanhui_sanjiao_ok 5
1  
1  1  
1  2  1  
1  3  3  1  
1  4  6  4  1 

相关文章

网友评论

      本文标题:shell实现永辉三角

      本文链接:https://www.haomeiwen.com/subject/lweiuftx.html