#coding: GBK
X_RANGE = (2..9) #第一个数的范围
Y_RANGE = (2..9) #第二个数的范围
MULT_FORMULA_CNTS = 50 #乘法算式总的个数
DIV_FORMULA_CNTS = 50 #除法算式总的个数
LINE_CNTS = 4 #每行算式个数
res = {}
while res.size < MULT_FORMULA_CNTS do
x = rand(X_RANGE)
y = rand(Y_RANGE)
if x != 1 && y != 1
key = sprintf("%d*%d", x, y)
type = rand(1..3)
str = sprintf("( ) × %d = %-2d", y, x*y) if type == 1
str = sprintf("%d × ( ) = %-2d", x, x*y) if type == 2
str = sprintf("%d × %d = ( )", x, y) if type == 3
res[key] = str
end
end
while res.size < (MULT_FORMULA_CNTS + DIV_FORMULA_CNTS) do
x = rand(X_RANGE)
y = rand(Y_RANGE)
if x != 1 && y != 1
type = rand(1..3)
key = sprintf("%d_%d", x, y)
str = sprintf("( ) ÷ %d = %d", x, y)if type == 1
str = sprintf("%-2d ÷ ( ) = %d", x*y, y)if type == 2
str = sprintf("%-2d ÷ %d = ( )", x*y, x)if type == 3
res[key] = str
end
end
out_tab = res.to_a
line = 0
out = {}
out_size = 0
while out_size < (MULT_FORMULA_CNTS+DIV_FORMULA_CNTS) do
i = rand(0..(MULT_FORMULA_CNTS+DIV_FORMULA_CNTS-1))
out[i] = i
if out_size != out.size
printf(out_tab[i][1])
line = line + 1
if line == LINE_CNTS
puts ""
puts ""
line = 0
else
printf(" ")
end
out_size = out.size
end
end