Files
Yudong Jin 2f561d3d51 feat(ko): add the Korean online edition (#2006)
* feat(ko): add the complete Korean online edition

* Use UTF-8 for Python CI output on Windows

* Remove print-book promotion from the Korean site

* fix(ko): preserve emphasis before Korean particles
2026-10-29 03:46:43 +08:00

63 lines
2.2 KiB
Zig

// File: unbounded_knapsack.zig
// Created Time: 2023-07-15
// Author: codingonion ([email protected])
const std = @import("std");
// 무한 배낭: 동적 계획법
fn unboundedKnapsackDP(comptime wgt: []i32, val: []i32, comptime cap: usize) i32 {
comptime var n = wgt.len;
// dp 테이블을 초기화한다
var dp = [_][cap + 1]i32{[_]i32{0} ** (cap + 1)} ** (n + 1);
// 상태 전이
for (1..n + 1) |i| {
for (1..cap + 1) |c| {
if (wgt[i - 1] > c) {
// 배낭 용량을 초과하면 물건 i를 선택하지 않는다
dp[i][c] = dp[i - 1][c];
} else {
// 물건 i를 선택하지 않는 경우와 선택하는 경우 중 더 큰 값
dp[i][c] = @max(dp[i - 1][c], dp[i][c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);
}
}
}
return dp[n][cap];
}
// 무한 배낭: 공간을 최적화한 동적 계획법
fn unboundedKnapsackDPComp(comptime wgt: []i32, val: []i32, comptime cap: usize) i32 {
comptime var n = wgt.len;
// dp 테이블을 초기화한다
var dp = [_]i32{0} ** (cap + 1);
// 상태 전이
for (1..n + 1) |i| {
for (1..cap + 1) |c| {
if (wgt[i - 1] > c) {
// 배낭 용량을 초과하면 물건 i를 선택하지 않는다
dp[c] = dp[c];
} else {
// 물건 i를 선택하지 않는 경우와 선택하는 경우 중 더 큰 값
dp[c] = @max(dp[c], dp[c - @as(usize, @intCast(wgt[i - 1]))] + val[i - 1]);
}
}
}
return dp[cap];
}
// 실행 코드
pub fn main() !void {
comptime var wgt = [_]i32{ 1, 2, 3 };
comptime var val = [_]i32{ 5, 11, 15 };
comptime var cap = 4;
// 동적 계획법
var res = unboundedKnapsackDP(&wgt, &val, cap);
std.debug.print("배낭 용량을 초과하지 않는 물건의 최대 가치: {}\n", .{res});
// 공간을 최적화한 동적 계획법
res = unboundedKnapsackDPComp(&wgt, &val, cap);
std.debug.print("배낭 용량을 초과하지 않는 물건의 최대 가치: {}\n", .{res});
_ = try std.io.getStdIn().reader().readByte();
}