- 論壇徽章:
- 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 # 瑞典人養(yǎng)狗
- judge 1;(( ${_TMP3[$_N]} != 0 )) && continue 1 # 抽PallMall香煙的人養(yǎng)鳥
- judge 2;if (( $_N \< 4 )) && (( ${_TMP3[$[$_N+1]]} == 2 ));then : # 抽Blends香煙的人住在養(yǎng)貓人的隔壁
- elif (( $_N \> 0 )) && (( ${_TMP3[$[$_N-1]]} == 2 ));then :
- else continue 1
- fi
- judge 3;if (( $_N \< 4 )) && (( ${_TMP3[$[$_N+1]]} == 1 ));then : # 養(yǎng)馬的人住在抽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 編輯 ] |
|