前言
这一次讲述的实验是拟解决生活中常见的问题:背包问题。
在物品集合种选择合适的物品放入背包,在放入背包中的物品总重量不超过背包容量的前提下,希望背包的物品总价值最大。
说实话,一开始看到题目和题目要求,是真的不知道怎么入手,等到写代码的时候自己的逻辑也比较绕,也可能是因为需要可视化的原因吧,如果是控制台,可能显示的就没那么麻烦了
一、题目说明
背包问题呢,有很多种解决方式,目前我们讲述的是贪心算法
以前在学数据结构时候,贪心算法就是噩梦,现在一开始就要贪心算法,一开始看到题目的时候还是很怕的。
题目呢,就是我们自己输入背包的容量,然后输入物品个数和每个物品的重量和价值,看看能不能将物品装入背包,并且找出给定实例的最优解
emmmm 是不是这样子看起来还蛮简单的
我们往下接着看
二、贪心算法
求解步骤、分析
根据我们输入的物品的重量和价值,
就可以求出每个物品的单位重量的价值=(价值/重量),简单来说就是物品贵重嘛
贪心算法:就是尽可能的装入背包,尽可能将单位重量的价值高的、多的装入背包,(就是先将贵重物品装入背包
单位重量价值最高的装入背包了之后呢,就选单位重量价值第二高的,然后以此类推,直到背包没满没有物品了,不然就直到背包满
所以会有出现装入比例是小数的情况
接下来来看代码
我是使用Android来呈现可视化的
三、代码实现
主界面
activity_main.xml
垂直线性布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:inputType="number"
android:hint="背包容量"/>
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="10dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/weight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext"
android:inputType="number"
android:hint="物品重量"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="10dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/values"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="物品价值"
android:imeOptions="actionNone"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/define"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
android:textColor="#CC9933"
app:cornerRadius="12dp"
app:strokeWidth="2dp"
app:strokeColor="#CC9933"
style="@style/materialButton"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButton
android:id="@+id/greed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="贪心算法"
android:textColor="#CC9933"
app:cornerRadius="12dp"
app:strokeWidth="2dp"
app:strokeColor="#CC9933"
style="@style/materialButton"
/>
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/goodsRecycler"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<style name="materialButton" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
<item name="android:textColor">#CC9933</item>
<item name="android:paddingLeft">25dp</item>
<item name="android:paddingRight">25dp</item>
<item name="android:paddingTop">10dp</item>
<item name="android:paddingBottom">10dp</item>
<item name="android:layout_marginLeft">10dp</item>
<item name="backgroundTint">@color/white</item>
<item name="icon">@drawable/right</item>
<item name="iconTint">#CC9933</item>
<item name="iconSize">20dp</item>
<item name="iconPadding">10dp</item>
</style>
这里呢 写了三个输入框,分别是用来输入背包容量和物品的重量和价值
最后使用按钮确定
然后将输入的物品重量和价值在下面显示
然后是RecyclerView中,使用CardView
recy_main.xml
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
app:cardCornerRadius="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="3dp"
android:id="@+id/goodsCard">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_weight="1"
android:layout_width="m