CCI: Representing N cents

Given an infinite number of quarters (25 cents), dimes (10 cents), nickels (5 cents) and pennies (1 cent), write code to calculate the number of ways of representing n cents.

简单的DFS可以解决,书中的理解起来感觉有些复杂。不过可以放在一起对比验证 - DFS确实慢啊,大数据多半不能接受,加上一个简单的剪枝效果可以好很多,不过还是不如书上的解法快。

void makeChange(int sum, int *coins, int n, int start, int &result) {
	if (sum < 0) {
		return;
	}
	if (sum == 0) {
		++result;
		return;
	}

	for (int i = start; i < n; ++i) {
		// Only one solution if coin equals to 1
		if (i == n - 1) {
			++result;
		}
		else {
			makeChange(sum - coins[i], coins, n, i, result);
		}
	}
}

int make_change(int n, int denom){
    int next_denom = 0;
    switch (denom){
    case 25:
        next_denom = 10;
        break;
    case 10:
        next_denom = 5;
        break;
    case 5:
        next_denom = 1;
        break;
    case 1:
        return 1;
    }
    int ways = 0;
    for(int i = 0; i*denom <= n; ++i)
        ways += make_change(n - i*denom, next_denom);
    return ways;
}

int main(int argc, char **argv) {
	int coins[] = {25, 10, 5, 1};
	int result = 0;
	time_t s1 = time(NULL);
	makeChange(8000, coins, 4, 0, result);
	double diff = difftime(time(NULL), s1);
	cout << "The numbers to make change is: " << result << " and the time used is: " << diff << endl;
	s1 = time(NULL);
	result = make_change(8000, 25);
	diff = difftime(time(NULL), s1);
	cout << "The numbers to make_change is: " << result << " and the time used is: " << diff << endl;

	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这段内核日志中的错误信息涉及相机的 CCI(Camera Control Interface)和 EEPROM(Electrically Erasable Programmable Read-Only Memory)模块。根据日志信息,可以看出发生了以下错误: 1. CAM-CCI: cam_cci_read: 1453 CCI1_I2C_M1_Q1 ERROR with Slave 0xa2 这个错误表示在与从设备地址为0xa2的设备进行通信时发生了错误。 2. CAM-CCI: cam_cci_read_bytes: 1816 CCI1_I2C_M1 Failed to read rc:-22 这个错误表示在读取数据时发生了错误,返回值为-22。 3. CAM-CCI: cam_cci_core_cfg: 2023 rc: -22 这个错误表示在配置相机控制接口时发生了错误,返回值为-22。 4. CAM-EEPROM: cam_eeprom_read_memory: 114 read failed rc -22 这个错误表示在读取 EEPROM 存储器中的数据时发生了错误,返回值为-22。 5. CAM-EEPROM: cam_eeprom_pkt_parse: 1329 read_eeprom_memory failed 这个错误表示在解析 EEPROM 数据包时发生了错误。 6. CAM-EEPROM: cam_eeprom_driver_cmd: 1540 Failed in eeprom pkt Parsing 这个错误表示在 EEPROM 驱动程序命令中解析数据包时发生了错误。 7. CAM-EEPROM: cam_eeprom_subdev_ioctl: 70 Failed in Driver cmd: -22 这个错误表示在 EEPROM 子设备的 IOCTL 命令中发生了错误,返回值为-22。 总体来说,这些错误都与相机的 CCI 和 EEPROM 模块的读取、配置和通信有关。可能的原因包括硬件故障、驱动程序问题或通信问题。你可能需要仔细检查相关的硬件连接、驱动程序和配置,以解决这些错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值