shell实现杨辉三角

shell实现杨辉三角

[root@linhexiao shellTest]# cat yanghuisanjiao.sh 
#!/bin/bash
export LANG="zh_CN.GB18030"
#create by 林鹤霄QQ:858993860
#mail:[email protected]

#judge input  如果启动脚本时没有指定杨辉三角的最大长度就提示输入
if [ -z $1 ];then     #-z $1字符串的长度是0的话,返回真
	read -p "Input Max Int Lines:" MAX   # read -p 打印一行字符串,用MAX接收输入
else
	MAX=$1
fi

#judge int
[ -n "`echo $MAX|sed 's/[0-9]//g'`" ] && \
echo "you input not a int!" && exit 1

#requir <10
[ $MAX -ge 10 ] && echo "you input number ge 10!" && exit 1

#start
a[0]=1
for((i=0;i<=MAX;i++))
do
	for((j=$i;j>0;j--))
	do
		((a[$j]+=a[$j-1]))
	done
	
	for((j=0;j<$[MAX-$i];j++))
	do
		if [ $MAX -le 10 ];then
			echo -en "\t"
		else
			echo -n " "
		fi
	done
	
	for((j=0;j<$i;j++))
	do
		if [ $MAX -lt 10 ];then
			echo -en "\t\t"${a[${j}]}
		else
			echo -n ${a[$j]}
		fi
	done
echo
done

猜你喜欢

转载自linhexiao.iteye.com/blog/2288454