题目(一)
有1 2 3 4 四位数,任意组合有多少种互不相同且无重复的数字,分别是什么?
shell代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#!/bin/bashfor 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 代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#!/bin/bashfor 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 |
第一个题对着这个图完全可以想出来,一共有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 代码如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
#!/bin/bashpre_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:"
readpre_1pre_2if [ $f -ne 0 ];then
echo "Ping Year"
else echo "Run Year"
fi |
本文转自 大雪儿 51CTO博客,原文链接:http://blog.51cto.com/dingxue/1969132,如需转载请自行联系原作者