贪心算法 ——硬币兑换、区间调度、

硬币兑换:

from book:挑战程序设计竞赛


思路:优先使用大面额兑换即可

Go 复制代码
package main

import "fmt"

func main() {
	results := []int{}//记录每一种数额的张数
	A := 620
	B := A//备份
	cnts := 0 //记录至少需要多少张
	nums := []int{1, 5, 10, 50, 100, 500}
	limits := []int{3, 2, 1, 3, 0, 2} //面额张数限制

	for i := len(nums) - 1; i >= 0; i-- {
		//nums_i 最多使用n张
		n := my_min(A/nums[i], limits[i])
		cnts += n
		results = append(results, n)
		A -= nums[i] * n
	}
	fmt.Print("A:\n", B, "cnts:\n", cnts)
	fmt.Print(results)

}

func my_min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

区间调度:

思路:

为了完成尽可能多的工作:需要i-1项工作尽可能的早结束,i项任务尽早的开始

相关推荐
Savior`L2 小时前
二分算法及常见用法
数据结构·c++·算法
mmz12073 小时前
前缀和问题(c++)
c++·算法·图论
努力学算法的蒟蒻3 小时前
day27(12.7)——leetcode面试经典150
算法·leetcode·面试
甄心爱学习4 小时前
CSP认证 备考(python)
数据结构·python·算法·动态规划
kyle~5 小时前
排序---常用排序算法汇总
数据结构·算法·排序算法
AndrewHZ5 小时前
【遥感图像入门】DEM数据处理核心算法与Python实操指南
图像处理·python·算法·dem·高程数据·遥感图像·差值算法
CoderYanger5 小时前
动态规划算法-子序列问题(数组中不连续的一段):28.摆动序列
java·算法·leetcode·动态规划·1024程序员节
有时间要学习5 小时前
面试150——第二周
数据结构·算法·leetcode
liu****6 小时前
3.链表讲解
c语言·开发语言·数据结构·算法·链表