• 1068 Find More Coins


    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

    Sample Input 1:

    1. 8 9
    2. 5 9 8 7 2 3 4 1

    Sample Output 1:

    1 3 5
    

    Sample Input 2:

    1. 4 8
    2. 7 2 4 3

    Sample Output 2:

    No Solution

    一开始使用 dfs+剪枝 测试点6超时了,特判一下所有硬币加起来也小于m的情况就可以过了:

    1. #include
    2. #include
    3. using namespace std;
    4. int n, m, a[10010], ans[10010], cnt, sum, total;
    5. bool vis[10010], flag;
    6. void dfs(int x) {
    7. if (flag) {
    8. return;
    9. }
    10. if (sum == m) {
    11. flag = 1;
    12. for (int i = 0; i < cnt; i++) {
    13. printf("%d", ans[i]);
    14. if (i != cnt - 1) {
    15. printf(" ");
    16. }
    17. }
    18. return;
    19. }
    20. if (x >= n || sum > m) {//剪枝
    21. return;//无解
    22. }
    23. if (sum + a[x] <= m && (!vis[x])) { //有选或者不选两种
    24. vis[x] = 1;
    25. sum += a[x];
    26. int t = cnt;
    27. ans[cnt++] = a[x];
    28. dfs(x + 1);
    29. cnt = t;
    30. sum -= a[x];
    31. vis[x] = 0;
    32. dfs(x + 1);
    33. } else { //只有不选这一种
    34. dfs(x + 1);
    35. }
    36. }
    37. int main() {
    38. scanf("%d %d", &n, &m);
    39. for (int i = 0; i < n; i++) {
    40. scanf("%d", &a[i]);
    41. total += a[i];
    42. }
    43. if (total < m) {
    44. printf("No Solution");
    45. return 0;
    46. }
    47. sort(a, a + n);
    48. dfs(0);
    49. if (!flag) {
    50. printf("No Solution");
    51. }
    52. return 0;
    53. }

  • 相关阅读:
    基于51单片机温湿度控制器proteus仿真设计
    SAP UI5 Form 和 Simple Form 的设计规范
    深入理解Java虚拟机读书笔记--5 JVM工具
    13.8 - 软件测试工作量及成本估算 3.9 - 软件测试成本估算示例
    【OFDM】多径信道下OFDM通信系统误码率仿真附matlab代码
    C++ 学习(六)函数 及 函数的分文件编写
    内网渗透之内网信息收集(七)
    Hadoop之HDFS——【模块一】元数据架构
    Java——迷你图书管理器(JDBC+MySQL+Apache DBUtils)
    微信小程序如何在切换页面后原页面状态不变
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/126167464