Linux用shell判断闰年,shell整理(31)===判断平年闰年和嵌套循环的小例子

题目(一)

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

shell代码如下:

#!/bin/bash

for i in `seq 4`

do

for x in `seq 4`

do

for y in `seq 4 `

do

[ $x -eq $i  ] && continue

[ $x -eq $y  ] && continue

[ $i -eq $y  ] && continue

echo $i$x$y

done

done

done

输出如下一共有24 种:

[[email protected] ding]# bash 11.sh

123

124

132

134

142

143

213

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

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

[[email protected] ding]# bash 12.sh

123

124

134

234

[[email protected] ding]#

shell 代码如下:

#!/bin/bash

for i in `seq 4`

do

for x in `seq $i 4`

do

for y in `seq $x 4`

do

[ $i -eq $x ] && continue

[ $x -eq $y  ] && continue

echo $i$x$y

done

done

done

05aacc134c50712555317ec0dcb488e9.png

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

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

判断规则:

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

输出如下:

[[email protected] ding]# bash 14.sh

Input:1996

Run Year

[[email protected] ding]# bash 14.sh

Input:2000

Run Year

[[email protected] ding]# bash 14.sh

Input:1990

Ping Year

[[email protected] ding]# bash 14.sh

Input:1997

Ping Year

[[email protected] ding]#

shell 代码如下:

#!/bin/bash

pre_1() {

a=${#REPLY}

[ -z $REPLY ] && echo -e "\033[32m plz Input! \033[0m" && exit 0

[ $a -lt 4 ] && echo "Plz Input more four num!" && exit 0

}

pre_2() {

b=`echo $REPLY |cut -c 3-4`

d=`echo $REPLY |cut -c 1-2`

c=$(($b%100))        #判断后两位是俩0,这是一种方法

[ $c -eq 0  ] && [ $(($d%4)) -eq 0 ] && echo "Run Year" && exit 0

[ $c -eq 0  ] && [ $(($d%4)) -ne 0 ] && echo "Ping Year" && exit 0

f=$(($b%4))

}

echo -n "Input:"

read

pre_1

pre_2

if [ $f -ne 0 ];then

echo "Ping Year"

else

echo "Run Year"

fi

时间: 09-24

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值