shell整理31===判断平年闰年和嵌套循环的小例子

题目(一)

有1 2 3 4 四位数,任意组合有多少种互不相同且无重复的数字,分别是什么?

shell代码如下:

#!/bin/bash

foriin`seq4`
do
forxin`seq4`
do
foryin`seq4`
do
[$x-eq$i]&&continue
[$x-eq$y]&&continue
[$i-eq$y]&&continue
echo$i$x$y


done
done
done

输出如下一共有24 种:

[root@localhost ding]# bash 11.sh

123

124

132

134

142

143

213

............

例如:我们把题目改一下,还是1 2 3 4 输出如下规律

[root@localhost ding]# bash 12.sh

123

124

134

234

[root@localhost ding]#

shell 代码如下:

#!/bin/bash

foriin`seq4`
do
forxin`seq$i4`
do
foryin`seq$x4`
do
[$i-eq$x]&&continue
[$x-eq$y]&&continue
echo$i$x$y
done
done
done


wKioL1nLOJjQK5SrAAB53A4ypI8621.png-wh_50


一个题对着这个图完全可以想出来,一共有64种,但是我们加上判断什么的最终筛选出24种

(二)用户任意输入四个数字,判断是平年还是闰年

判断规则:

看年份末两位不能被4整除的是平年,被4整除但末两位都是0 的,要看前两位能被4整除的是闰年,不能整除的是平年。例如:1996 、2000闰年 1990 、1997平年

输出如下:

[root@localhost ding]# bash 14.sh

Input:1996

Run Year

[root@localhost ding]# bash 14.sh

Input:2000

Run Year

[root@localhost ding]# bash 14.sh

Input:1990

Ping Year

[root@localhost ding]# bash 14.sh

Input:1997

Ping Year

[root@localhost ding]#


shell 代码如下:

#!/bin/bash


pre_1(){
a=${#REPLY}
[-z$REPLY]&&echo-e"\033[32mplzInput!\033[0m"&&exit0
[$a-lt4]&&echo"PlzInputmorefournum!"&&exit0
}

pre_2(){
b=`echo$REPLY|cut-c3-4`
d=`echo$REPLY|cut-c1-2`
c=$(($b%100))#判断后两位是俩0,这是一种方法
[$c-eq0]&&[$(($d%4))-eq0]&&echo"RunYear"&&exit0
[$c-eq0]&&[$(($d%4))-ne0]&&echo"PingYear"&&exit0
f=$(($b%4))
}
echo-n"Input:"
read
pre_1
pre_2

if[$f-ne0];then
echo"PingYear"
else
echo"RunYear"
fi

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...