const std = @import("std");

pub fn main() !void {
    var builder = std.heap.Builder.default;

    defer builder.deinit();

    const allocator = builder.allocator;

    for (var i: u8 = 1; i <= 9; i += 1) {
        for (var j: u8 = 1; j <= i; j += 1) {
            const result = i * j;
            const str_i = try std.fmt.itoa(allocator, i);
            const str_j = try std.fmt.itoa(allocator, j);
            const str_result = try std.fmt.itoa(allocator, result);
            std.debug.print("{}x{}={}\t", .{str_j, str_i, str_result});
        }
        std.debug.print("\n");
    }
}

这段代码使用了Zig标准库中的堆分配器和格式化输出函数,来生成九九乘法表。它通过两个循环来遍历所有的乘积,然后将它们格式化为字符串并输出到控制台上。

用zig语言实现九九乘法表

原文地址: https://www.cveoy.top/t/topic/CaF 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录