RyanKao2001 2020-01-13 00:32 采纳率: 100%
浏览 449
已采纳

新手c语言编程穷举问题

有四种面值不一样的货币分别是10元,5元,2元,1元。现在给你一个钱数你能求求它的组成方法都有多少吗?

运行时间尽可能短

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-01-13 11:10
    关注

    问题解决的话,请点下采纳,谢谢

    // Q1051673.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    
    #include <stdio.h>
    #include <string.h>
    
    void solve(int * seed, int nseed, int * cand, int ncand, int target)
    {
        if (target == 0)
        {
            for (int i = 0; i < nseed; i++)
            {
                printf("%d ", seed[i]);
            }
            printf("\n");
            return;
        }
        for (int i = 0; i < ncand; i++)
        {
            if (target >= cand[i] && cand[i] <= seed[nseed - 1])
            {
                int * seed1 = new int[nseed + 1];
                memcpy(seed1, seed, nseed * sizeof(int));
                seed1[nseed] = cand[i];
                solve(seed1, nseed + 1, cand, ncand, target - cand[i]);
                delete[] seed1;
            }
        }
    }
    
    int main()
    {
        int cand[4] = { 10, 5, 2, 1 };
        int target = 25; //钱数
        for (int i = 0; i < 4; i++)
        {
            int * seed = new int[1];
            seed[0] = cand[i];
            solve(seed, 1, cand, 4, target - seed[0]);
            delete[] seed;
        }
    }
    

    10 10 5
    10 10 2 2 1
    10 10 2 1 1 1
    10 10 1 1 1 1 1
    10 5 5 5
    10 5 5 2 2 1
    10 5 5 2 1 1 1
    10 5 5 1 1 1 1 1
    10 5 2 2 2 2 2
    10 5 2 2 2 2 1 1
    10 5 2 2 2 1 1 1 1
    10 5 2 2 1 1 1 1 1 1
    10 5 2 1 1 1 1 1 1 1 1
    10 5 1 1 1 1 1 1 1 1 1 1
    10 2 2 2 2 2 2 2 1
    10 2 2 2 2 2 2 1 1 1
    10 2 2 2 2 2 1 1 1 1 1
    10 2 2 2 2 1 1 1 1 1 1 1
    10 2 2 2 1 1 1 1 1 1 1 1 1
    10 2 2 1 1 1 1 1 1 1 1 1 1 1
    10 2 1 1 1 1 1 1 1 1 1 1 1 1 1
    10 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    5 5 5 5 5
    5 5 5 5 2 2 1
    5 5 5 5 2 1 1 1
    5 5 5 5 1 1 1 1 1
    5 5 5 2 2 2 2 2
    5 5 5 2 2 2 2 1 1
    5 5 5 2 2 2 1 1 1 1
    5 5 5 2 2 1 1 1 1 1 1
    5 5 5 2 1 1 1 1 1 1 1 1
    5 5 5 1 1 1 1 1 1 1 1 1 1
    5 5 2 2 2 2 2 2 2 1
    5 5 2 2 2 2 2 2 1 1 1
    5 5 2 2 2 2 2 1 1 1 1 1
    5 5 2 2 2 2 1 1 1 1 1 1 1
    5 5 2 2 2 1 1 1 1 1 1 1 1 1
    5 5 2 2 1 1 1 1 1 1 1 1 1 1 1
    5 5 2 1 1 1 1 1 1 1 1 1 1 1 1 1
    5 5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    5 2 2 2 2 2 2 2 2 2 2
    5 2 2 2 2 2 2 2 2 2 1 1
    5 2 2 2 2 2 2 2 2 1 1 1 1
    5 2 2 2 2 2 2 2 1 1 1 1 1 1
    5 2 2 2 2 2 2 1 1 1 1 1 1 1 1
    5 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1
    5 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1
    5 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    5 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    5 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    5 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    2 2 2 2 2 2 2 2 2 2 2 2 1
    2 2 2 2 2 2 2 2 2 2 2 1 1 1
    2 2 2 2 2 2 2 2 2 2 1 1 1 1 1
    2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1
    2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1
    2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1
    2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1
    2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
    Press any key to continue . . .

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 镍氢电池充电器设计实物使用原理
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号