编辑代码

function a1_15(n) 
    local p = 4.75; local tax = 0.03; local extra = 0.10;
    local res = 4.74 * n
    res = res * (1 + 0.03 + 0.10)
    return res
end

function a1_20(n)
    local res;
    res = n * 9 * 12345679
    print(9 * 12345679)
    return res;
end

function a1_25a(n)
    local space, i, k, maxWidth; 
    i = 0; space = 0; maxWidth = n + n - 1;
    while (i < n) do
        space = (maxWidth - (i + i - 1)) // 2;
        while (space > 0) do io.write(' '); space = space - 1; end
        k = 0;
        while (k < i) do io.write("*"); io.write(' '); k = k + 1; end
        print();
        i = i + 1;
    end
end

function a1_27()
    local s, e, n, d, m, o, r, y, n1, n2, n3;
    m = 1
    for s = 0, 9 do
        for e = 0, 9 do
            for n = 0, 9 do
                for d = 0, 9 do
                    for o = 0, 9 do
                        for r = 0, 9 do
                            for y = 0, 9 do
                                n1 = 1000 * s + 100 * e + 10 * n + d;
                                n2 = 1000 * m + 100 * o + 10 * r + e;
                                n3 = 10000 * m + 1000 * 0 + 100 * n + 10 * e + y;  
                                if ((d + e) % 10 == y and n1 + n2 == n3) 
                                then
                                    print(n1 .. ' + ' .. n2 .. ' = ' .. n3);
                                    -- return -- 有很多组结果
                                end
                            end
                        end
                    end
                end
            end
        end
    end
end

function a1_29(a, b)
    local c, i;  i = 0
    io.write(a .. ', ' .. b .. ', ')
    while (i < 3) do
        c = a * b; a = b; b = c; i = i + 1; io.write(c, ', ')
    end
end

function a1_30()
    local i; i = 1;
    while (i < 7) do print(i / 7); i = i + 1; end
    print()
    print(1/7); 
    io.write(' '); print(3/7); 
    print(2/7); 
    io.write(' '); print(6/7); 
    print(4/7); 
    io.write(' '); print(5/7);
end

function a1_30()
    local m;
    m = 9485 // 4;
    while (m < 9485 and m * 4 - 74 - 23 - 86 ~= 9485) do m = m + 1; end
    print(m)
    return m 
end

function a2()
    -- a ^ b == exp(b * ln(a))
    local a, b;
    a =  5 ^ 8;  -- ^ 是乘方
    b = math.exp(8 * math.log(5)) -- 底数都是e
    print(a)
    print(b)
    a = math.floor(a); b = math.floor(b); -- 竟然差1 说明四舍五入不同
    print(a, b)
    a = math.tointeger(a); b = math.tointeger(b); -- 竟然差1 
    print(a, b)
    print(math.tointeger(a) == math.tointeger(b))
end
-- a2(2,3)
function StringEx()
    s = "abcdef"
    for i = 1, 5 do 
        print(string.sub(s, i)); 
        print(string.sub(s, i, i))
        print(string.byte(s, i))    
    end
end

function plist(xs)
    s = '{'
    for i = 1, #xs do 
        s = s .. xs[i]
        if (i ~= #xs) then
            s = s .. ', ' 
        end
    end
    s = s .. '}'
    print(s)
end
xs = {1, 2, 3}
plist(xs)


 function permgen (a, n)
    if n == 0 then
    printResult(a)
    else
    for i=1,n do

        -- put i-th element as the last one
        a[n], a[i] = a[i], a[n]

        -- generate all permutations of the other elements
        permgen(a, n - 1)

        -- restore i-th element
        a[n], a[i] = a[i], a[n]

    end
    end
end

 function printResult (a)
    for i,v in ipairs(a) do
    io.write(v, " ")
    end
    io.write("\n")
end

permgen ({1,2,3,4}, 2)