shell practice --PAT 1005 title: Continue (3n + 1) guess

Kharazi (Callatz) conjecture description has been given in 1001. In this topic, the situation is somewhat more complicated.

When we verify Kharazi guess when, in order to avoid double counting, the number can be recorded at each recursive encountered. For example  n- = time to verify 3, we need to calculate 3,5,8,4,2,1, when we  n- = time for verification 5,8,4,2, can be directly determined Kharazi guess authenticity, without double counting, because it has the number 4 3 when encountered in the validation, we are called 5,8,4,2 3 "coverage" of the number. We call a number in the column of a number  n is "critical number", if  n can not be covered by other figures in the series.

Now given to a series of numbers to be verified, we only need to verify a few key number of them, you do not have to be repeated to verify the remaining numbers. Your task is to find these key figures, according to output them in descending order.

Input formats:

Each test comprises a test input, the first row is given a positive integer  K ( <), the second row is given  a positive integer number K to be authenticated mutually different  n ( from 1) value, a space number separated.

Output formats:

Each test case output per line, in descending order of output key figures. Separated by a space between the numbers, but after a row last number with no spaces.

Sample input:

6
3 5 6 7 8 11

Sample output:

7 6
Analysis:
  1. The value should be determined by the associative array is the minimum value.
  2. To understand the issue covered the relationship, you can use a script to run about 1001, respectively, these numbers can be found, for 3568, the existence of a relationship between the contained contains: 6 → 3 → 5 → 8 , for the 711, there is a relationship 7 → 11's.
It can be determined by the number n which is not in an array on a number of coverage can be determined whether the problem exists.
wyf349 @ ubuntu: ~ / user / study_shell $ ./1005.callatz.err1.sh # increasing intermediate value a storage array, printing 
INPUT n-:. 3 
. 5. 1 2. 8. 4 
wyf349 Ubuntu @: ~ / User / study_shell $ ./1005 .callatz.err1.sh 
INPUT n-:. 5 
. 8. 4 2. 1 
wyf349 Ubuntu @: ~ / User / study_shell ./1005.callatz.err1.sh $ 
INPUT n-:. 6 
. 3. 1. 5 2. 8. 4 
wyf349 Ubuntu @: ~ / User / study_shell ./1005.callatz.err1.sh $ 
INPUT n-:. 8 
. 4. 1 2

  Specific code as follows, had not +1

# / bin / the bash 
# Callatz check rules 
func_check () { 
        IF [expr $ `2` -eq% n-0] 
        the then 
                n-echo =` "$ n-/ 2" | BC ` 
        the else 
                n-=` echo "((. 3 the n-$ *) +1) / 2 "| bc` 
        fi 
} 

the Read -p "the iNPUT str:" str # character input group here does not solve the problem of multi-line input directly, using the -d parameter can be multiple lines, but you need to specify terminator, simply ignoring the first line 

declare -A arr_n # define an associative array arr_n 

for n-in $ (echo $ STR) 
do 
    IF [-a $ 0 $ n-n--gt -LT-100]; the then echo "$ n-IS right! ! "; the else echo" NO NUM "&& Exit. 1; Fi 2> / dev / null 
    ARR = (n-$) # as the first element of the input 
	I =. 1 
	the while [$ n-!   = 1 ]   
	do
		func_check
		arr[$i]=$n
		let i++
	done
	#echo ${arr[@]}
	J in $ {arr for [@]}   
	do 
		the number of [$ j] ++ # statistics associative arrays let arr_n, the same increment 
	DONE      
DONE 

for arr_title in $ {arr_n [@]!} 
do 
        echo $ str | grep -wq $ arr_title # determines whether the number of the input digital 
        iF [-eq $ 0?]   
        the then 
                echo $ $ {} {arr_title arr_n [$ {arr_title}]} | awk '{iF (== $ 2. 1). 1 Print $} ' 
        Fi 
DONE | tac # reverse order there are two problems: ① the pipe is always processed, the processing part to pass a portion of, the value tac sorted insufficiency; ②tac is in reverse order, it is necessary to use Sort | tac or directly sort - r reverse order

  The question now, is no way to solve the problem shell directly read multiple lines (do not change at the end, do not use text redirection)

Guess you like

Origin www.cnblogs.com/wyf-349/p/11245649.html