枚举 PROBLEM 伪代码总结
problem 1:生理周期
simple enumerate
1,读入p,e,i,d 2, loop1: k=d+1,increase k to 20000,if (k-p)%23=0, break loop1 3. loop2: increase k by 23 to 20000, if (k-e)%28=0, break loop2 4, loop3: increase k by 23*28 to 20000, if (k-i)%33=0,break loop3
-------------------------------------------------------------------------------------------------
problem 2:称硬币
model with several variables
1, read all input information 2, loop1: enumerate coin "a" to coin "l" if coin is heavy : break and print if coin is light: break and print
以字符串数组存储称量的结果。每次称量时,天平左右最多有6 枚硬币。因此,字符串的长度需要为7,最后一位存储字符串的结束符’\0’,便于程序代码中使用字符串操作函数。char left[3][7], right[3][7], result[3][7];
-------------------------------------------------------------------------------------
problem 3:完美立方
the solution in search space is not unique
1, store all cube[i] 2, loop1: for a =6 to n loop2: for b=a+1 to a-1 judge if cube[a]<cube[b]+cube[b+1]+cube[b+2] break; loop3: for c=b+1 to a judge ............ellipsis ........
避免对一个整数的立方的重复计算。[2 N]中的每个整数i,在整个需要判断的四元组
序列中都反复出现。每出现一次,就要计算一次它的立方。在开始完美立方等式的判断
之前,先用一个数组保存[2 N]中的每个整数的立方值。在判断四元组(a、b、c、d)是
否满足完美立方等式的要求时,直接使用存储在数组中的立方值。
---------------------------------------------------------------------------------------------
problem 4: 熄灯问题
traverse search space
1, input all the information 2, enumerate all conditions of the press in first line (press[1][1] to press[1][6]): loop1 guess : according to the condition of puzzle[][] and the condition of press[][] in the first line, guess the other conditions of press[][], and judge whether this press[1][] is available 3 , output
问题中的矩阵是一个5×6 的矩阵,但在程序实现中采用一个6×8 的矩阵表示。目的是
为了在计算press[r+1][c]时,能够用一个共同的公式
press[r+1][c] = ( puzzle[r][c] + press[r][c] + press[r-1][c] + press[r][c-1] + press[r][c+1] ) mod 2
这样可以简化代码的实现。否则计算press 边界、内部元素的值时,分别需要不同的代码。
------------------------------------------------------------------------------------
problem 5:讨厌的青蛙
optimize judging condition
STL的用法:
qsort(plants, n, sizeof(PLANT), myCompare); //plants[] ; n is the num of plants[]; myCompare is // comparison function ; PLANT is struct
bsearch(&plant, plants, n, sizeof(PLANT), myCompare) //plant is the address of the point u want to find
1, input all information chose struct to store the point information 2, sort the all the point plant [1] to plant[n] 3, enumerate 2 point plant[i] , plant[j] , i<j 3.1 distance=plant[j]-plant[i] 3.2 if plant[i] - distance is NOT out of border. continue to next numerating condition 3.3 if plant[i] + max * distance is out of y border, continue 3.4 if plant[i] + max*distance is out of x border, break 3.5 find the steps between plant[i] to border 3.6 if max<steps max=steps 4, output