蓝桥杯(购物券)

头文件

#include <cstdio>  

#include <cstdlib>

#include <list>

#include <string>

#include <iterator>

#include <iostream>

using namespace std;

 

两个数组

int *a,*b;    

组数 

int n;

方法解数 

int count = 0;  

用来存放解

list<string> meanprintf;

 

void print()  

{  

int i;

初始化一个字符串

     string temp("");

   中间变量的字符

     char cc;

   生成结果

     for(i = 0; i < n; i++){  

       cc=b[i]+'0';

          temp+=cc;

          if(i!=n-1)

                temp+=' ';

          }

   将结果保存

     meanprintf.push_back(temp);

}  

容量种数

void problem9(int total,int size)  

{  

   if(size != n )  

{  

   背包满了

       if(total == 0)//判断背包是否正装满  

       {  

           count++;//存放可行方案的个数  

           print();//输出此可行解  

        }  

背包没有满一定要注意回溯

       else 

       {  

           problem9(total,size + 1);//未放入物品asize】的情况  

           if(total - a[size] >= 0)//判断放入asize】后,是否会超出背包的承载  

           {  

               b[size]++;//使得asize】对应物品的数量加1  

               problem9(total - a[size],size);//放入物品a[size]的情况  

               b[size]--;//使得相应的物品数量减1  

           }  

       }  

   }  

 

}  

void main()  

{  

int i,j;  

输入种数

   scanf("%d",&n);  

   a = (int*)malloc(sizeof(int) * n);   

   b = (int*)malloc(sizeof(int) * n);  

   for(i = 0; i < n; i++)  

       scanf("%d",&a[i]);  

   for(j = 0; j < n; j++)  

       b[j] = 0;  

   problem9(1000,0);  

   printf("%d\n",count);  

     for (list<string>::iterator iter=meanprintf.begin();iter!=meanprintf.end();++iter)

          cout<<*iter<<endl;

}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值