作业帮 > 综合 > 作业

求斐波那契数列第n项值得shell编程?

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/17 23:38:37
求斐波那契数列第n项值得shell编程?
[oldboy@daiqingyangs scripts]$ cat list.sh
#!/bin/bash
#set -x
if [ $# -eq 0 ]
then
exit
fi
count=1
index=$1
while [ $count -le $index ]
do
if [ $count -eq 1 ]
then
arr[$count]=1
count=$(($count+1))
continue
elif [ $count -eq 2 ]
then
arr[$count]=1
count=$(($count+1))
continue
fi
pre1=$(($count-1))
pre2=$(($count-2))
arr[$count]=$((${arr[$pre1]}+${arr[$pre2]}))
count=$(($count+1))
done
echo ${arr[$index]}
[oldboy@daiqingyangs scripts]$ bash list.sh 10
55