列舉法 填數

2021-07-07 08:54:22 字數 3363 閱讀 5683

在奧數中會有如下的題目:     

算 法 描 述 題

x 算題 題 題 題 題  

解法:

假設每個數字都從0-9迴圈

直到計算出的結果與真實結果相等

pesudeocode:

算=i1:法=i2:描=i3:述=i4:題=i5

for i1=1 to 9

for i2=0 to 9

for i3=0 to 9

for i4=0 to 9

for i5=0 to 9

m1=i1*10^4+i2*10^3+i3*10^2+i4*10^1+i5

m2=i11

r1=i5*10^4+i5*10^3+i5*10^2+i5*10^1+i5

if m1=r1 then output resutl

'//程式用以解決填數問題

'//只考慮正整數的情況

'//列舉法,效率比較低,總步數=9*10*10*10*9=81000

sub numberfill()

dim r1&, out1(), n&, step&

dim m1&, i1&, i2&, i3&, i4&, i5&

redim out1(1 to 100, 1 to 6)

with sheet4

for i1 = 1 to 9 '被乘數不可能是0,所以從1-9

for i2 = 0 to 9

for i3 = 0 to 9

for i4 = 0 to 9

for i5 = 1 to 9 '結果不為0,所以從1-9

m1 = i1 * 10 ^ 4 + i2 * 10 ^ 3 + i3 * 10 ^ 2 + i4 * 10 + i5

r1 = i5 * 10 ^ 5 + i5 * 10 ^ 4 + i5 * 10 ^ 3 + i5 * 10 ^ 2 + i5 * 10 + i5

step = step + 1

if m1 * i1 = r1 then

n = n + 1

out1(n, 1) = i1: out1(n, 2) = i2

out1(n, 3) = i3: out1(n, 4) = i4

out1(n, 5) = i5: out1(n, 6) = r1

end if

next i5

next i4

next i3

next i2

next i1

'output

.cells(2, 9) = step

.cells(2, 10).resize(ubound(out1), ubound(out1, 2)) = out1

end with

end sub

當然也可以將題目變形一下:

填入適當的運算子,使等式成立

5 5 5 5 5 =5

兩點需要注意

a.除號右側的數字不能為0

b.乘除的運算級別高於加減

tip:

可以為變數left和right儲存計算值

'//程式用以解決填運算子

'//只考慮正整數的情況

sub operfill()

dim out2(), cout&, chr$, nout& '儲存輸出字段

dim i(4), j&, op1&, op2&, op3&, op4& '陣列arr儲存運算子+-*/,變數

dim num(5), n& '儲存需計算的5個數字

dim leftnum!, rightnum! '儲存中間結果

dim sign&, oper(0 to 4) '儲存累加運算子

dim result! '結果

with sheet4

'讀入運算子

oper(0) = "": oper(1) = "+": oper(2) = "-": oper(3) = "*": oper(4) = "/"

'讀入數字

for n = 1 to 5

num(n) = .cells(23, n)

next n

result = right(.cells(23, 6), 1)

redim out2(1 to 1000, 1 to 2)

'main program

for op1 = 1 to 4

i(1) = op1

for op2 = 1 to 4

i(2) = op2

for op3 = 1 to 4

i(3) = op3

for op4 = 1 to 4

i(4) = op4

'初始賦值

leftnum = 0

rightnum = num(1)

sign = 1

for j = 1 to 4

select case oper(i(j))

case "+" '"+"

leftnum = leftnum + sign * rightnum

sign = 1

rightnum = num(j + 1)

case "-" '"-"

leftnum = leftnum + sign * rightnum

sign = -1

rightnum = num(j + 1)

case "*" '"*"

rightnum = rightnum * num(j + 1)

case "/" '"/"

rightnum = rightnum / num(j + 1)

end select

next j

if leftnum + sign * rightnum = result then

'儲存結果

cout = cout + 1

for nout = 1 to 4

out2(cout, 1) = cout

out2(cout, 2) = out2(cout, 2) & num(nout) & oper(i(nout))

next nout

out2(cout, 2) = out2(cout, 2) & num(5) & "=" & result

end if

next op4

next op3

next op2

next op1

'output

.cells(30, 1).resize(ubound(out2), ubound(out2, 2)) = out2

end with

end sub

方陣填數問題 模擬法

基礎 方陣填數 easy time limit 1000ms memory limit 65536k total submit 226 accepted 109 description 在乙個n n的方陣中,填入1 2 n n個數,並要求構成如下的格式 例 n 5 13 14 15 16 1 12 ...

快速排序(Quick Sort) 挖坑填數法

前面的博文講了氣泡排序 選擇排序 插入排序,今天我們談談快速排序!快速排序的基本思想是 1 先從序列中取出乙個數作為基準數。2 分割槽過程,將比這個數大的數全放到它的右邊,小於或等於它的數全放到它的左邊。分割槽的方式多樣,但一定要保證基準數左邊的數比它大 小 右邊的數比它小 大 3 再對左右區間重複...

C 蛇形填數

蛇形填數 問題描述 在n n方陳裡填入1,2,n n,要求填成蛇形。例如n 4時方陳為 10 11 12 1 9 16 13 2 8 15 14 3 7 6 5 4 輸入直接輸入方陳的維數,即n的值。n 100 輸出輸出結果是蛇形方陳。問題分析 我們定義乙個二維陣列,開始x 0,y n 1,即第一行...