-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
41 changed files
with
1,569 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
def coin_change_min_weight(v, w, y): | ||
|
||
n = len(v) # 硬币种类数 | ||
|
||
# 初始化动态规划表 | ||
F = [float('inf')] * (y + 1) | ||
F[0] = 0 | ||
|
||
# 用于记录选择的硬币 | ||
choice = [[0] * n for _ in range(y + 1)] | ||
|
||
# 动态规划过程 | ||
for j in range(1, y + 1): | ||
for k in range(n): | ||
if v[k] <= j: | ||
if F[j - v[k]] + w[k] < F[j]: | ||
F[j] = F[j - v[k]] + w[k] | ||
choice[j] = choice[j - v[k]].copy() | ||
choice[j][k] += 1 | ||
|
||
return F[y], choice[y] | ||
|
||
# 测试代码 | ||
if __name__ == "__main__": | ||
v_test = [1, 4, 6, 8] # 硬币面值 | ||
w_test = [1, 2, 4, 6] # 硬币重量 | ||
y_test = 12 # 需要支付的总价值 | ||
|
||
min_weight, coin_used = coin_change_min_weight(v_test, w_test, y_test) | ||
|
||
print(f"最小总重量: {min_weight}") | ||
print("使用的硬币:") | ||
for i, count in enumerate(coin_used): | ||
if count > 0: | ||
print(f"面值 {v_test[i]}: {count} 个") |
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
\documentclass{article} | ||
\usepackage{fancyhdr} | ||
\usepackage{extramarks} | ||
\usepackage{amsmath} | ||
\usepackage{amsthm} | ||
\usepackage{amsfonts} | ||
\usepackage{tikz} | ||
\usepackage[UTF8, scheme=plain, punct=plain, zihao=false]{ctex} | ||
\usepackage[shortlabels]{enumitem} | ||
\usetikzlibrary{automata,positioning} | ||
\usepackage{algorithm} | ||
\usepackage{algpseudocode} | ||
\usepackage{float} | ||
\usepackage{lipsum} | ||
|
||
\renewcommand{\algorithmicrequire}{\textbf{Input:}} % Use Input in the format of Algorithm | ||
\renewcommand{\algorithmicensure}{\textbf{Output:}} % Use Output in the format of Algorithm | ||
\newcommand{\True}{\textbf{true}} | ||
\newcommand{\False}{\textbf{false}} | ||
\usepackage{graphics} | ||
\usepackage{epsfig} | ||
\usepackage{indentfirst} | ||
|
||
% 算法跨页 | ||
\makeatletter | ||
\newenvironment{breakablealgorithm} | ||
{\begin{center} | ||
\refstepcounter{algorithm}% New algorithm | ||
\hrule height.8pt depth0pt \kern2pt% \@fs@pre for \@fs@ruled | ||
\renewcommand{\caption}[2][\relax]{% Make a new \caption | ||
{\raggedright\textbf{\ALG@name~\thealgorithm} ##2\par}% | ||
\ifx \relax##1\relax % #1 is \relax | ||
\addcontentsline{loa}{algorithm}{\numberline{\thealgorithm}##2}% | ||
\else % #1 is not \relax | ||
\addcontentsline{loa}{algorithm}{\numberline{\thealgorithm}##1}% | ||
\fi | ||
\kern2pt\hrule\kern2pt | ||
} | ||
}{\kern2pt\hrule\relax | ||
\end{center} | ||
} | ||
\makeatother | ||
|
||
\topmargin=-0.45in | ||
\evensidemargin=0in | ||
\oddsidemargin=0in | ||
\textwidth=6.5in | ||
\textheight=9.0in | ||
\headsep=0.25in | ||
|
||
\linespread{1.1} | ||
|
||
\pagestyle{fancy} | ||
\lhead{\hmwkAuthorName} | ||
\chead{\hmwkClass\ \hmwkTitle} | ||
\rhead{\firstxmark} | ||
\lfoot{\lastxmark} | ||
\cfoot{\thepage} | ||
|
||
\renewcommand\headrulewidth{0.4pt} | ||
\renewcommand\footrulewidth{0.4pt} | ||
|
||
\newcommand{\enterProblemHeader}[1]{ | ||
\nobreak\extramarks{}{Problem \arabic{#1} continued on next page\ldots}\nobreak{} | ||
\nobreak\extramarks{Problem \arabic{#1} (continued)}{Problem \arabic{#1} continued on next page\ldots}\nobreak{} | ||
} | ||
|
||
\newcommand{\exitProblemHeader}[1]{ | ||
\nobreak\extramarks{Problem \arabic{#1} (continued)}{Problem \arabic{#1} continued on next page\ldots}\nobreak{} | ||
\stepcounter{#1} | ||
\nobreak\extramarks{Problem \arabic{#1}}{}\nobreak{} | ||
} | ||
|
||
\setcounter{secnumdepth}{0} | ||
\newcounter{partCounter} | ||
\newcounter{homeworkProblemCounter} | ||
\setcounter{homeworkProblemCounter}{1} | ||
\nobreak\extramarks{Problem \arabic{homeworkProblemCounter}}{}\nobreak{} | ||
|
||
\newenvironment{homeworkProblem}[1][-1]{ | ||
\ifnum#1>0 | ||
\setcounter{homeworkProblemCounter}{#1} | ||
\fi | ||
\section{\huge{Problem} \arabic{homeworkProblemCounter}} | ||
\setcounter{partCounter}{1} | ||
\enterProblemHeader{homeworkProblemCounter} | ||
}{ | ||
\exitProblemHeader{homeworkProblemCounter} | ||
} | ||
|
||
% Homework Details | ||
\newcommand{\hmwkTitle}{第1次作业} | ||
\newcommand{\hmwkDueDate}{Oct. 29, 2020} | ||
\newcommand{\hmwkClass}{计算机算法设计与分析} | ||
\newcommand{\hmwkAuthorName}{张三 202180132...} | ||
|
||
\title{ | ||
\vspace{2in} | ||
\textmd{\textbf{\Huge{\hmwkClass} | ||
\\ \Large{\hmwkTitle}}}\\ | ||
\vspace{3in} | ||
} | ||
|
||
\author{\Large{\textbf{张三}}\\\textbf{2020280132.....}} | ||
\date{} | ||
|
||
\renewcommand{\part}[1]{\textbf{\large Part \Alph{partCounter}}\stepcounter{partCounter}\\} | ||
|
||
\newcommand{\alg}[1]{\textsc{\bfseries \footnotesize #1}} | ||
|
||
% For derivatives | ||
\newcommand{\deriv}[1]{\frac{\mathrm{d}}{\mathrm{d}x} (#1)} | ||
|
||
% For partial derivatives | ||
\newcommand{\pderiv}[2]{\frac{\partial}{\partial #1} (#2)} | ||
|
||
% Integral dx | ||
\newcommand{\dx}{\mathrm{d}x} | ||
|
||
% Alias for the Solution section header | ||
\newcommand{\solution}{\textbf{\large Solution}} | ||
|
||
% Probability commands: Expectation, Variance, Covariance, Bias | ||
\newcommand{\E}{\mathrm{E}} | ||
\newcommand{\Var}{\mathrm{Var}} | ||
\newcommand{\Cov}{\mathrm{Cov}} | ||
\newcommand{\Bias}{\mathrm{Bias}} | ||
|
||
\begin{document} | ||
\alglanguage{pseudocode} | ||
\maketitle | ||
|
||
\pagebreak | ||
\begin{homeworkProblem} | ||
\textbf{\large{Assignment Problem4}} | ||
\\\solution\\ | ||
\begin{breakablealgorithm} | ||
\begin{algorithmic}[1] | ||
\caption{Algorithm} | ||
\Require{$A,B$}\Comment{A is ..., B is ...} | ||
\Ensure{$[C,D]$}\Comment{C is ..., D is ...} | ||
\State Initialize $A=-1,B=-1$; | ||
\State \Call{SubFunction}{A,B} | ||
\If{A} | ||
\State return C; | ||
\EndIf | ||
\Function{SubFunction}{...} | ||
\Comment{$如果...就...$} | ||
\EndFunction | ||
\end{algorithmic} | ||
\end{breakablealgorithm} | ||
\subsection{Subproblem Reduced Graph} | ||
以...为例,查看图...。 | ||
\subsection{Proof of the correctness} | ||
要证明的循环不变量: | ||
\begin{itemize} | ||
\item Initialization: | ||
\item Maintenance: | ||
\item Termination: | ||
\end{itemize} | ||
|
||
\证明: | ||
\begin{itemize} | ||
\item Initialization: | ||
\item Maintenance: | ||
\item Termination: | ||
\end{itemize} | ||
|
||
\subsection{Analysis of Complexity} | ||
\textbf{时间复杂度} | ||
|
||
$O(x)$。 | ||
|
||
\textbf{空间复杂度} | ||
\end{homeworkProblem} | ||
|
||
\end{document} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
% 测试代码 | ||
v_test = [1, 4, 6, 8]; % 硬币面值 | ||
w_test = [1, 2, 4, 6]; % 硬币重量 | ||
y_test = 12; % 需要支付的总价值 | ||
[minWeight, coinUsed] = coinChangeMinWeight(v_test, w_test, y_test); | ||
fprintf('最小总重量: %d\n', minWeight); | ||
fprintf('使用的硬币:\n'); | ||
for i = 1:length(v_test) | ||
if coinUsed(i) > 0 | ||
fprintf('面值 %d: %d 个\n', v_test(i), coinUsed(i)); | ||
end | ||
end | ||
function [minWeight, coinUsed] = coinChangeMinWeight(v, w, y) | ||
n = length(v); % 硬币种类数 | ||
% 初始化动态规划表 | ||
F = inf(n+1, y+1); | ||
F(:, 1) = 0; % 修正:将第一列(总价值为0)初始化为0 | ||
% 用于记录选择的硬币 | ||
choice = zeros(n+1, y+1); | ||
% 动态规划过程 | ||
for k = 2:n+1 | ||
for j = 1:y+1 | ||
% 不选择第k种硬币 | ||
F(k, j) = F(k-1, j); | ||
|
||
% 如果可以选择第k种硬币 | ||
if j > v(k-1) | ||
if F(k, j-v(k-1)) + w(k-1) < F(k, j) | ||
F(k, j) = F(k, j-v(k-1)) + w(k-1); | ||
choice(k, j) = 1; | ||
end | ||
end | ||
end | ||
end | ||
% 回溯找出使用的硬币 | ||
minWeight = F(n+1, y+1); | ||
coinUsed = zeros(1, n); | ||
j = y+1; | ||
for k = n+1:-1:2 | ||
while choice(k, j) == 1 | ||
coinUsed(k-1) = coinUsed(k-1) + 1; | ||
j = j - v(k-1); | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
设有n种不同面值的硬币,第i中硬币的币值是 v_i,重量是w_i=1,2,,,,n。且现在购买总价值为y的某些商品,需要用这些硬币付款,若每种钱币使用的个数不限,问如何选择付款的方式使得付出的总重量最轻?给出该问题的数学描述,并设计一个求解该问题的算法,给出算法的伪码描述,以及实现代码。假设问题的输入实例是:\nu_1=1,\quad\nu_2=4,\quad\nu_3=6,\quad\nu_4=8\text{ ; }w_1=1,\quad w_2=2,\quad w_3=4,\quad w_4=6,\quad y=12 | ||
给出算法所获得的付款方式。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
\relax | ||
\@writefile{toc}{\contentsline {section}{\numberline {1}问题一:简述分治策略的思想}{1}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {1.1}分治策略的基本步骤}{1}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {1.2}分治策略的特点}{1}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {1.3}分治策略的应用实例}{2}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {1.4}分治策略的优势与局限性}{2}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {section}{\numberline {2}问题二:简述动态规划的思想}{3}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {2.1}动态规划的基本原理}{3}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {2.2}动态规划的实现方法}{3}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {2.3}动态规划的步骤}{4}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {2.4}动态规划的典型应用}{4}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {2.5}动态规划的优势与局限性}{5}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {section}{\numberline {3}问题三:简述贪心法的设计思想}{5}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {3.1}贪心法的核心思想}{5}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {3.2}贪心法的设计步骤}{6}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {3.3}贪心法的特点}{6}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {3.4}贪心法的应用场景}{6}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {section}{\numberline {4}问题四:简述回溯算法的设计思想}{7}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {4.1}回溯算法的核心思想}{7}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {4.2}回溯算法的设计步骤}{7}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {4.3}回溯算法的特点}{8}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {4.4}回溯算法的应用场景}{8}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {4.5}回溯与深度优先搜索的关系}{8}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {section}{\numberline {5}问题五}{9}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {5.1}问题描述}{9}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {5.2}数学描述}{9}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {5.3}算法设计}{10}{}\protected@file@percent } | ||
\@writefile{loa}{\contentsline {algorithm}{\numberline {1}{\ignorespaces Min Weight Algorithm}}{11}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {5.4}MATLAB实现}{11}{}\protected@file@percent } | ||
\@writefile{lol}{\contentsline {lstlisting}{\numberline {1}MATLAB代码}{11}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {5.5}运行结果}{12}{}\protected@file@percent } | ||
\@writefile{toc}{\contentsline {subsection}{\numberline {5.6}结论}{12}{}\protected@file@percent } | ||
\gdef \@abspage@last{12} |
Oops, something went wrong.