【PAT】【Advanced Level】1068. Find More Coins (30)

本文介绍了一道经典的01背包问题FindMoreCoins,并详细解析了如何找出最优支付方案的具体算法实现过程。输入包括硬币数量及所需支付金额,输出为能够恰好支付该金额的最小面额序列。

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

1068. Find More Coins (30)

时间限制
150 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Eva loves to collect coins from all over the universe, including some other planets like Mars. One day she visited a universal shopping mall which could accept all kinds of coins as payments. However, there was a special requirement of the payment: for each bill, she must pay the exact amount. Since she has as many as 104 coins with her, she definitely needs your help. You are supposed to tell her, for any given amount of money, whether or not she can find some coins to pay for it.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive numbers: N (<=104, the total number of coins) and M(<=102, the amount of money Eva has to pay). The second line contains N face values of the coins, which are all positive numbers. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the face values V1 <= V2 <= ... <= Vk such that V1 + V2 + ... + Vk = M. All the numbers must be separated by a space, and there must be no extra space at the end of the line. If such a solution is not unique, output the smallest sequence. If there is no solution, output "No Solution" instead.

Note: sequence {A[1], A[2], ...} is said to be "smaller" than sequence {B[1], B[2], ...} if there exists k >= 1 such that A[i]=B[i] for all i < k, and A[k] < B[k].

Sample Input 1:
8 9
5 9 8 7 2 3 4 1
Sample Output 1:
1 3 5
Sample Input 2:
4 8
7 2 4 3
Sample Output 2:
No Solution
原题链接:

https://www.patest.cn/contests/pat-a-practise/1068

https://www.nowcoder.com/pat/5/problem/4119

思路:

0 1 背包

难点在于输出满足条件的最优方案


还在学习中,占坑等着写题解。。。


CODE:

#include<iostream>
#include<cstring>
#include<algorithm>
#define N 10010
#define M 210
using namespace std;
int w[N];
int r[M];
bool f[N][M];
bool cmp(int a, int b)
{
	return a>b;
}
int main()
{
	memset(f,0,sizeof(f));
	int n,m;
	cin>>n>>m;
	for (int i=1;i<=n;i++)
	{
		cin>>w[i];
	}
	sort(w+1,w+n+1,cmp);
	r[0]=0;
	for (int i=1; i<=n; i++)  
        for (int j=m; j>=w[i]; j--)  
        {  
            if (r[j]<=r[j-w[i]]+w[i])
            {
            	f[i][j]=1;
            	r[j]=r[j-w[i]]+w[i];
            }
        }  
	//cout<<r[m]<<endl;
    if (r[m]!=m)
    {
    	cout<<"No Solution";
    }
    else
    {
		int v=m, index=n;
        int s=0;
		while(v > 0) {
            if(f[index][v] == true) {
                if (s!=0) cout<<" ";
				cout<<w[index];
                v -= w[index];
                s++;
            }
            index--;
        }
	}
	return 0;
}





在Python中,我们可以使用OpenCV库来进行图像处理,包括霍夫变换(Hough Transform)来检测直线和圆形。首先,需要安装`opencv-python`库。以下是一个简单的示例,演示如何读取图像、边缘检测以及应用霍夫变换: ```python import cv2 import numpy as np # 读取图像 img1 = cv2.imread('computer.jpg') img2 = cv2.imread('coins.jpg') # 检查图像是否加载成功 if img1 is None or img2 is None: print("图片无法加载") else: # 转换为灰度图像 gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY) gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY) # Canny边缘检测 edges1 = cv2.Canny(gray1, 50, 150) # 边缘阈值可以根据实际情况调整 edges2 = cv2.Canny(gray2, 50, 150) # 对边缘图像应用霍夫变换寻找直线 lines1 = cv2.HoughLinesP(edges1, 1, np.pi / 180, 100, minLineLength=100, maxLineGap=50) lines2 = cv2.HoughLinesP(edges2, 1, np.pi / 180, 100, minLineLength=100, maxLineGap=50) # 对边缘图像应用霍夫变换寻找圆形 circles1 = cv2.HoughCircles(edges1, cv2.HOUGH_GRADIENT, dp=1, minDist=20, param1=50, param2=30, minRadius=0, maxRadius=0) circles2 = cv2.HoughCircles(edges2, cv2.HOUGH_GRADIENT, dp=1, minDist=20, param1=50, param2=30, minRadius=0, maxRadius=0) # 可视化结果 for line in lines1: x1, y1, x2, y2 = line[0] cv2.line(img1, (x1, y1), (x2, y2), (0, 255, 0), 2) for circle in circles1: if circle is not None and len(circle): cv2.circle(img1, (circle[0][0], circle[0][1]), circle[0][2], (0, 0, 255), 2) for line in lines2: x1, y1, x2, y2 = line[0] cv2.line(img2, (x1, y1), (x2, y2), (0, 255, 0), 2) for circle in circles2: if circle is not None and len(circle): cv2.circle(img2, (circle[0][0], circle[0][1]), circle[0][2], (0, 0, 255), 2) # 显示原始图像和检测后的图像 cv2.imshow("Computer Image with Lines", img1) cv2.imshow("Coins Image with Circles", img2) cv2.waitKey(0) cv2.destroyAllWindows()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值