- 论坛徽章:
- 0
|
楼主很强,我也坐了一下那个题,出结果用了14秒多没有楼主的快,贴上来凑热闹。
- #!/bin/bash
- # x0 country: [0]English [1]Sweden [2]Denmark [3]Norway [4]German
- # x1 color: [0]red [1]green [2]white [3]yellow [4]blue
- # x2 drink: [0]tea [1]coffee [2]milk [3]beer [4]water
- # x3 smoke: [0]pallmall [1]dunhill [2]blends [3]prince [4]blueMaster
- # x4 pet: [0]dog [1]bird [2]cat [3]horse [4]fish
- #---------------------------------#
- # 假设所有条件代码如上.房子的顺序为(1,2,3,4,5)
- # 问题分析 ,根据排列组合公式推算 本题的可能性为 (5*4*3*2*1)^5 = 120^5 = 24883200000
- # 其中隐含的第六个条件为房子的顺序 我们假设为 1 2 3 4 5 故不参与排列。
- # 在n中选出m个出来排列 n*(n-1)*....(n-m+1)
- # 在n中选出m个出来组合 n*(n-1)*....(n-m+1)/(1*2*3*....m)
- # 如果全部过滤的可能性不大,根据条件逐次过滤。
- # Variables and Function definition
- country=(english sweden denmark norway german );
- color=( red green white yellow blue );
- drink=( tea coffee milk beer water );
- smoke=( palmall dunhill blends prince bluemaster);
- pet=( dog bird cat horse fish );
- function arrange {
- echo {0..4},{0..4},{0..4},{0..4},{0..4}|tr ' ' '\n'|sed -e '/.*\([0-9]\).*\1.*/d'
- }
- function judge {
- for i in $(seq 0 4);do
- if (( ${_TMP[$i]} == $1 ));then
- _N=$i; break 1
- fi
- done
- }
- function delidding {
- echo '#--------------------------------------------#'
- echo -n -e "house1\thouse2\thouse3\thouse4\thouse5\n\n"
- x7=($(echo ${_TMP0[@]} ${_TMP1[@]} ${_TMP2[@]} ${_TMP3[@]} ${_TMP4[@]}))
- for i in $(seq 0 24);do
- if (( $i \<5 ));then
- A=${x7[$i]};echo -n -e ${country[$A]}"\t"
- elif (( $i \<10 ));then
- (( $i == 5 )) && echo -e "\n"
- A=${x7[$i]};echo -n -e ${color[$A]}"\t"
- elif (( $i \<15 ));then
- (( $i == 10 )) && echo -e "\n"
- A=${x7[$i]};echo -n -e ${drink[$A]}"\t"
- elif (( $i \<20 ));then
- (( $i == 15 )) && echo -e "\n"
- A=${x7[$i]};echo -n -e ${smoke[$A]}"\t"
- elif (( $i \<25 ));then
- (( $i == 20 )) && echo -e "\n"
- A=${x7[$i]};echo -n -e ${pet[$A]}"\t"
- fi
- done
- echo -e "\n"'#--------------------------------------------#'
- }
- # Program main
- for x0 in $(arrange);do
- _TMP0=($(echo ${x0}|tr ',' ' '))
- (( ${_TMP0[0]} != 3 )) && continue 1 # 威人住第一间房子
- for x1 in $(arrange);do
- _TMP1=($(echo ${x1}|tr ',' ' '))
- _TMP=(${_TMP1[@]})
- (( ${_TMP1[1]} != 4 )) && continue 1 # 挪威人住蓝色房子隔壁
- judge 0;(( ${_TMP0[$_N]} != 0 )) && continue 1 # 英国人住红色房子
- judge 1;_G=$_N;judge 2;_W=_N
- # (( $_G > $_W )) && continue 1 # 绿色房子在白色房子左面,这里有两种不同的理解,绿白是否相邻,如果不相邻用这句。
- (( $_W != $[$_G+1] )) && continue 1 # 绿色房子在白色房子左面 如果相邻用这句,不相邻的结果不唯一,有3种可能,这里假设相邻。
- for x2 in $(arrange);do
- _TMP2=($(echo ${x2}|tr ',' ' '))
- _TMP=(${_TMP2[@]})
- (( ${_TMP2[2]} != 2 )) && continue 1 # 住在中间房子的人喝牛奶
- judge 0;(( ${_TMP0[$_N]} != 2 )) && continue 1 # 丹麦人喝茶
- judge 1;(( ${_TMP1[$_N]} != 1 )) && continue 1 # 绿色房子主人喝咖啡
- for x3 in $(arrange);do
- _TMP3=($(echo ${x3}|tr ',' ' '))
- _TMP=(${_TMP3[@]})
- judge 3;(( ${_TMP0[$_N]} != 4 )) && continue 1 # 德国人抽prince香烟
- judge 1;(( ${_TMP1[$_N]} != 3 )) && continue 1 # 黄色房子主人抽Dunhill
- judge 4;(( ${_TMP2[$_N]} != 3 )) && continue 1 # 抽BlueMaster的人喝啤酒
- judge 2;if (( $_N \< 4 )) && (( ${_TMP2[$[$_N+1]]} == 4 ));then : # 抽Blends香烟人的有一个喝水的邻居
- elif (( $_N \> 0 )) && (( ${_TMP2[$[$_N-1]]} == 4 ));then :
- else continue 1
- fi
- for x4 in $(arrange);do
- _TMP4=($(echo ${x4}|tr ',' ' '))
- _TMP=(${_TMP4[@]})
- judge 0;(( ${_TMP0[$_N]} != 1 )) && continue 1 # 瑞典人养狗
- judge 1;(( ${_TMP3[$_N]} != 0 )) && continue 1 # 抽PallMall香烟的人养鸟
- judge 2;if (( $_N \< 4 )) && (( ${_TMP3[$[$_N+1]]} == 2 ));then : # 抽Blends香烟的人住在养猫人的隔壁
- elif (( $_N \> 0 )) && (( ${_TMP3[$[$_N-1]]} == 2 ));then :
- else continue 1
- fi
- judge 3;if (( $_N \< 4 )) && (( ${_TMP3[$[$_N+1]]} == 1 ));then : # 养马的人住在抽Dunhil香烟人的隔壁
- elif (( $_N \> 0 )) && (( ${_TMP3[$[$_N-1]]} == 1 ));then :
- else continue 1
- fi
- delidding
- done
- done
- done
- done
- done
复制代码
- [13:08 root@ xinyv]# time ./a.sh
- #--------------------------------------------#
- house1 house2 house3 house4 house5
- norway denmark english german sweden
- yellow blue red green white
- water tea milk coffee beer
- dunhill blends palmall prince bluemaster
- cat horse bird fish dog
- #--------------------------------------------#
- real 0m14.219s
- user 0m7.521s
- sys 0m6.889s
复制代码
[ 本帖最后由 xinyv 于 2008-1-18 13:48 编辑 ] |
|