[2_3_money] classic backpack problem

本文介绍了一种使用动态规划算法来解决奶牛社会中不同货币系统下构成特定金额的方法。通过对给定的几种面额硬币进行组合,计算出构成指定金额的不同方式的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Money Systems

The cows have not only created their own government but they have chosen to create their own money system. In their own rebellious way, they are curious about values of coinage. Traditionally, coins come in values like 1, 5, 10, 20 or 25, 50, and 100 units, sometimes with a 2 unit coin thrown in for good measure.

The cows want to know how many different ways it is possible to dispense a certain amount of money using various coin systems. For instance, using a system of {1, 2, 5, 10, ...} it is possible to create 18 units several different ways, including: 18x1, 9x2, 8x2+2x1, 3x5+2+1, and many others.

Write a program to compute how many ways to construct a given amount of money using supplied coinage. It is guaranteed that the total will fit into both a signed long long (C/C++) and Int64 (Free Pascal).

PROGRAM NAME: money

INPUT FORMAT

The number of coins in the system is V (1 <= V <= 25).

The amount money to construct is N (1 <= N <= 10,000).

Line 1:Two integers, V and N
Lines 2..:V integers that represent the available coins (no particular number of integers per line)

SAMPLE INPUT (file money.in)


3 10
1 2 5

OUTPUT FORMAT

A single line containing the total number of ways to construct N money units using V coins.

SAMPLE OUTPUT (file money.out)


10










We use dynamic programming to count the number of ways to make n cents with the given coins. If we denote the value of the kth coin by c_k, then the recurrence is:

   nway(n, k) = no. of ways to make n cents with the first k types of coins
   nway(n, k) = nway(n, k-1) + nway(n-c_k, k)

This just says the number of ways to make n cents with the first k coins is the number of ways to make n cents using the first k-1 coins (i.e., without using the kth coin) plus the number of ways to make n-c_k cents using the first k coins. For the second set of ways, we then add the kth coin to arrive at a total of n cents.

We keep track of the number of ways to total "n" cents in "nway", updating the array as we read the value of each coin.


/*
PROG: money
ID: rsc001
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define MAXTOTAL 10000

long long nway[MAXTOTAL+1];

void
main(void)
{
	FILE *fin, *fout;
	int i, j, n, v, c;

	fin = fopen("money.in", "r");
	fout = fopen("money.out", "w");
	assert(fin != NULL && fout != NULL);

	fscanf(fin, "%d %d", &v, &n);

	nway[0] = 1;
	for(i=0; i<v; i++) {
		fscanf(fin, "%d", &c);

		for(j=c; j<=n; j++)
			nway[j] += nway[j-c];
	}

	fprintf(fout, "%lld\n", nway[n]);
}


slower O(n^3) and O(n^4) solutions:

#include <cstdio>

long long dp[30][10010];
int v,n,a[30];

int main()
{
	freopen("money.in","r",stdin);
	freopen("money.out","w",stdout);
	scanf("%d%d",&v,&n);
	dp[0][0]=1;
	for(int i=1;i<=v;i++)
		scanf("%d",&a[i]);
	for(int i=1;i<=v;i++)
	{
		for(int j=0;j<a[i];j++)
			dp[i][j]=dp[i-1][j];
		for(int j=a[i];j<=n;j++)
			for(int k=j;k>=0;k-=a[i])
				dp[i][j]+=dp[i-1][k];
	}
	printf("%lld\n",dp[v][n]);
/*for(int i=1;i<=v;i++)for(int k=0;k<i;k++)for(int j=a[i];j<=n;j++)for(int t=j-a[i];t>=0;t-=a[i])dp[i][j]+=dp[k][t];long long ans=0;for(int i=1;i<=v;i++)ans+=dp[i][n];printf("%lld\n",ans);*/return 0;}




### ROS2 Galactic 中 cartographer_ros 的使用指南 #### 安装与配置 在 ROS2 Galactic 下安装 `cartographer_ros` 可通过源码编译完成。以下是具体操作方法: 1. 创建一个新的工作空间并克隆仓库: ```bash mkdir -p ~/ros2_galactic_ws/src cd ~/ros2_galctic_ws/src git clone https://github.com/ros2/cartographer_ros.git -b galactic-devel ``` 2. 编译依赖项并构建项目: ```bash vcs import < cartographer_ros/cartographer_ros.repos --recursive colcon build --symlink-install --packages-up-to cartographer_ros source install/setup.bash ``` 上述命令会下载必要的依赖包,并针对银河版 (Galactic) 进行适配[^1]。 3. 验证安装成功与否可以通过运行示例文件来测试: ```bash ros2 launch cartographer_ros demo_backpack_2d.launch.py ``` 此外,也可以启动 Rviz 查看效果: ```bash ros2 run rviz2 rviz2 -d ~/ros2_galactic_ws/src/cartographer_ros/cartographer_ros/configuration_files/demo_2d.rviz ``` 以上步骤展示了如何基于官方文档设置环境以及验证功能正常运作的情况[^4]。 #### 动态地图与导航集成 对于希望直接利用由 `cartographer_ros` 所产生的 SLAM 地图来进行路径规划的应用场景而言,无需额外转换至 PGM 文件再加载到 map_server 。而是可以直接订阅 `/map` 主题消息作为输入给全局或者局部成本映射器使用[^3]。 例如,在 AMCL 或者其他粒子滤波定位算法中,只需修改参数文件中的静态地图来源即可切换为实时更新的地图数据流: ```yaml global_costmap: static_layer: map_topic: /map ``` 这样做的好处在于能够即时反映环境中变化的部分,从而提高机器人应对复杂情况的能力。 #### 示例代码展示 下面给出一段简单的 Python 脚本例子说明怎样获取来自 Cartographer 发布的地图信息: ```python import rclpy from nav_msgs.msg import OccupancyGrid def callback(msg): print(f"Received new map with resolution {msg.info.resolution}") def main(args=None): rclpy.init(args=args) node = rclpy.create_node(&#39;map_subscriber&#39;) subscription = node.create_subscription( OccupancyGrid, &#39;/map&#39;, callback, 10) while rclpy.ok(): rclpy.spin_once(node) if __name__ == &#39;__main__&#39;: main() ``` 此脚本创建了一个节点用来监听 `/map` 主题上的新消息到达事件,并打印其分辨率属性值[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值