洛谷 p2089 会了递归之后- - 怎么那么喜欢呢

题目背景

猪猪hanke得到了一只鸡

题目描述

猪猪Hanke特别喜欢吃烤鸡(本是同畜牲,相煎何太急!)Hanke吃鸡很特别,为什么特别呢?因为他有10种配料(芥末、孜然等),每种配料可以放1—3克,任意烤鸡的美味程度为所有配料质量之和

现在,Hanke想要知道,如果给你一个美味程度,请输出这10种配料的所有搭配方案

输入输出格式

输入格式:
一行,n<=20

输出格式:
第一行,方案总数

第二行至结束,10个数,表示每种配料所放的质量

按字典序排列。

输入输出样例

输入样例#1:
11
输出样例#1:
10
1 1 1 1 1 1 1 1 1 2
1 1 1 1 1 1 1 1 2 1
1 1 1 1 1 1 1 2 1 1
1 1 1 1 1 1 2 1 1 1
1 1 1 1 1 2 1 1 1 1
1 1 1 1 2 1 1 1 1 1
1 1 1 2 1 1 1 1 1 1
1 1 2 1 1 1 1 1 1 1
1 2 1 1 1 1 1 1 1 1
2 1 1 1 1 1 1 1 1 1
说明

枚举

递归= = = = 。

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
int d[15];
int n;
int ans;
string sw[9000];
void bfs(int t,int s,int x)
{   d[t]=x;
    if(s==n&&t==10)
    {

        int i;
        for(i=1;i<=10;i++)
        {
            char a=(d[i]+'0');
            sw[ans]=sw[ans]+a;
           // cout<<sw[ans]<<' ';
        }
        //cout<<endl;
        ans++;
    }
    for(int i=1;i<=3&&t<=10;i++)
    {
        bfs(t+1,s+i,i);
    }
}


int main()
{

    while(cin>>n)
    {
        ans=0;
        bfs(0,0,0);
        sort(sw,sw+ans);
        cout<<ans<<endl;
        for(int i=0;i<ans;i++)
        {
            for(int j=0;j<10;j++)
            cout<<sw[i][j]<<' ';
            cout<<endl;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值