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 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体