[2_3_money] classic backpack problem

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;}




Cartographer_ros 是一个在 ROS 中使用 Google Cartographer 进行 2D 和 3D SLAM 的软件包。它提供了一个 ROS 节点,可以将 Cartographer 的输出与 ROS 生态系统中的其他软件包集成起来。 Cartographer_ros 的源代码框架如下: 1. launch 文件:Cartographer_ros 使用 launch 文件来配置和启动 ROS 节点。launch 文件包括 cartographer.launch、demo_backpack_2d.launch、demo_backpack_3d.launch 等。 2. ROS 节点:Cartographer_ros 的主要节点是 cartographer_node,它负责运行 Cartographer,并将数据发布到 ROS 话题上。cartographer_node 本身是由多个模块组成的,每个模块负责处理不同的任务。 3. ROS 话题:cartographer_node 将数据发布到 ROS 话题上,这些话题包括激光雷达数据、里程计数据、地图数据等。 4. Cartographer 模块:Cartographer 是 Cartographer_ros 的核心部分,它包括了多个模块,如 sensor_bridge、mapping、localization 等。这些模块负责处理传感器数据、建立地图和定位机器人。 5. ROS 服务:Cartographer_ros 还提供了一些 ROS 服务,比如 start_trajectory、finish_trajectory 等,这些服务可以用于控制 Cartographer 的运行。 6. 消息定义:Cartographer_ros 使用了自定义的消息类型,如 cartographer_ros_msgs/TrajectoryState、cartographer_ros_msgs/SubmapList 等。 总的来说,Cartographer_ros 的源代码框架是基于 ROS 架构设计的,它通过 ROS 节点、话题和服务来实现 Cartographer 的集成和控制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值