背景
扑克牌中斗牛的算法规则是:发五张牌,五张牌中如果有其中三张加起来为10的倍数则为有牛,剩下两张牌相加是多少就是牛几,加起来大于10的话,则取个位数部分。
脚本
#!/bin/bash
#从大到小排列输入的数字
echo "请输入五个不重复的自然数,范围为1-10"
read -p "请输入第一个自然数" one
read -p "请输入第二个自然数" two
if [ $one -gt $two ];then
a=$one
b=$two
else
b=$one
a=$two
fi
read -p "请输入第三个自然数" thr
if [ $thr -gt $a ];then
c=$b
b=$a
a=$thr
elif [ $thr -gt $b ];then
c=$b
b=$thr
else
c=$thr
fi
read -p "请输入第四个自然数" fou
if [ $fou -gt $a ];then
d=$c
c=$b
b=$a
a=$fou
elif [ $fou -gt $b ];then
d=$c
c=$b
b=$fou
elif [ $fou -gt $c ];then
d=$c
c=$fou
else
d=$fou
fi
read -p "请输入第五个自然数" fiv
if [ $fiv -gt $a ];then
e=$d
d=$c
c=$b
b=$a
a=$fiv
elif [ $fiv -gt $b ];then
e=$d
d=$c
c=$b
b=$fou
elif [ $fiv -gt $c ];then
e=$d
d=$c
c=$fiv
elif [ $fiv -gt $d ];then
e=$d
d=$fiv
else
e=$fiv
fi
echo "你输入的五个自然数分别为 $a $b $c $d $e"
#分辨是否有牛
if [ $[$[$a+$b+$c]%10] -eq 0 ];then
n=$[$d+$e]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$a+$b+$d]%10] -eq 0 ];then
n=$[$c+$e]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$a+$b+$e]%10] -eq 0 ];then
n=$[$c+$d]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$a+$c+$d]%10] -eq 0 ];then
n=$[$b+$e]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$a+$c+$e]%10] -eq 0 ];then
n=$[$b+$d]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$b+$c+$d]%10] -eq 0 ];then
n=$[$a+$e]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$b+$c+$e]%10] -eq 0 ];then
n=$[$a+$d]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$b+$d+$e]%10] -eq 0 ];then
n=$[$a+$c]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$c+$d+$e]%10] -eq 0 ];then
n=$[$a+$b]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
else
echo "$a $b $c $d $e没牛"
fi
思考
增添条件,先筛选出所有的不重复的五位数搭配,再进行斗牛的条件匹配
1.筛选出所有的不重复的五位数搭配
#!/bin/bash
for((a=10;a>=5;a--))
do
for((b=a-1;b>=4;b--))
do
for((c=b-1;c>=3;c--))
do
for((d=c-1;d>=2;d--))
do
for((e=d-1;e>=1;e--))
do
echo $a $b $c $d $e >> 5.txt
done
done
done
done
done
2.利用选出来的搭配进行判断并输出
#!/bin/bash
L=`cat 5.txt | wc -l`
for i in $(seq 1 $L)
do
a=`head -$i 5.txt | tail -1 | awk '{print $1}'`
b=`head -$i 5.txt | tail -1 | awk '{print $2}'`
c=`head -$i 5.txt | tail -1 | awk '{print $3}'`
d=`head -$i 5.txt | tail -1 | awk '{print $4}'`
e=`head -$i 5.txt | tail -1 | awk '{print $5}'`
#分辨是否有牛
if [ $[$[$a+$b+$c]%10] -eq 0 ];then
n=$[$d+$e]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$a+$b+$d]%10] -eq 0 ];then
n=$[$c+$e]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$a+$b+$e]%10] -eq 0 ];then
n=$[$c+$d]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$a+$c+$d]%10] -eq 0 ];then
n=$[$b+$e]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$a+$c+$e]%10] -eq 0 ];then
n=$[$b+$d]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$b+$c+$d]%10] -eq 0 ];then
n=$[$a+$e]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$b+$c+$e]%10] -eq 0 ];then
n=$[$a+$d]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$b+$d+$e]%10] -eq 0 ];then
n=$[$a+$c]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$c+$d+$e]%10] -eq 0 ];then
n=$[$a+$b]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
else
echo "$a $b $c $d $e没牛"
fi
done
3.综合两个脚本
#!/bin/bash
for((a=10;a>=5;a--))
do
for((b=a-1;b>=4;b--))
do
for((c=b-1;c>=3;c--))
do
for((d=c-1;d>=2;d--))
do
for((e=d-1;e>=1;e--))
do
#分辨是否有牛
if [ $[$[$a+$b+$c]%10] -eq 0 ];then
n=$[$d+$e]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$a+$b+$d]%10] -eq 0 ];then
n=$[$c+$e]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$a+$b+$e]%10] -eq 0 ];then
n=$[$c+$d]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$a+$c+$d]%10] -eq 0 ];then
n=$[$b+$e]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$a+$c+$e]%10] -eq 0 ];then
n=$[$b+$d]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$b+$c+$d]%10] -eq 0 ];then
n=$[$a+$e]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$b+$c+$e]%10] -eq 0 ];then
n=$[$a+$d]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$b+$d+$e]%10] -eq 0 ];then
n=$[$a+$c]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
elif [ $[$[$c+$d+$e]%10] -eq 0 ];then
n=$[$a+$b]
if [ $n -gt 10 ];then
n=$[$n-10]
fi
echo "$a $b $c $d $e是牛$n"
else
echo "$a $b $c $d $e没牛"
fi
done
done
done
done
done