分享脚本,同一个问题,php,python,shell的写法

关于写脚本,1 2 3 5 8 13 .............用脚本写出,第一百个数是什么

shell:

##########下面是脚本内容##########

#!/bin/bash
for((i=1;i<=100;i++))
do
if [ $i -eq 1 ];then
let s[1]=1
elif [ $i -eq 2 ];then
let s[2]=2
else
let s[$i]=s[i-1]+s[i-2]
fi
done
echo "$((s[100]))"

##########shell脚本到此为止####

值得注意的是,let做数学运算不用使用$,最后输出第一百个数的时候,要加上(()),否则只会输出s[100]

php

//////////////////下面是脚本内容/////////////////

<?php
for ($i=1; $i<=100; $i++)
{
if ($i==1)
{
$s[1]=1;
}
elseif ($i==2)
{
$s[2]=2;
}
else
{
$s[$i]=$s[$i-1]+$s[$i-2];
}
}
echo "$s[100]";
?>

////////////////////脚本到此结束///////////////////

值得注意,if的判断是用==或者是===,最好把每个if语句的内容都加{}。

python的写法

 d={1:1,2:2}

 i=3

 while i<=100 and i>=3:
      d[i]=d[i-1]+d[i-2]
     i+=1
 print d[100]

~~~~~~~~

d = [1,1]

for i  in range(101):

    d.append(d[i-1]+d[i-2])

print d[100]

原文地址:https://www.cnblogs.com/2myroad/p/3723910.html