洛谷P2089烤鸡

题目链接:https://www.luogu.org/problemnew/show/P2089

题目详情:

题目背景

猪猪hanke得到了一只鸡

题目描述

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

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

输入输出格式

输入格式:

一行,n<=5000

输出格式:

第一行,方案总数

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

按字典序排列。

如果没有符合要求的方法,就只要在第一行输出一个“0”

输入输出样例

输入样例#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 

说明

枚举

既然题目都提示考点是枚举了,那么久采用最简单直接的暴力破解法,莽啊

10个循环嵌套嘿嘿嘿

不过也不能直接莽,会超时,观察题目可以发现,本题存在大幅剪枝空间,总共10种配料,每种的范围在1-3克之间,也就意味着,最少得用10克配料,最多只能用30种配料,一下子把题目中n<=5000的范围缩小到10<=n<=30,这样就不会超时啦

#include<bits/stdc++.h>
using namespace std;

int arr[50010][15];

int main()
{
    int n;
    scanf("%d", &n);
    int x = min(n - 9, 3);//每种配料最多只能用3克
    
    if(n <= 9 || n > 30){//每种配料的范围在1-3之间小于等于9克或者大于30克均无法完成
        printf("0");
        return 0;
    }
    
    int cnt = 0, sum = 0;
    for(int a = 1; a <= x; a++){
        for(int b = 1; b <= x; b++){
            for(int c = 1; c <= x; c++){
                for(int d = 1; d <= x; d++){
                    for(int e = 1; e <= x; e++){
                        for(int f = 1; f <= x; f++){
                            for(int g = 1; g <= x; g++){
                                for(int h = 1; h <= x; h++){
                                    for(int i = 1; i <= x; i++){
                                        for(int j = 1; j <= x; j++){
                                            if(a+b+c+d+e+f+g+h+i+j == n){
                                                arr[cnt][0] = a, arr[cnt][1] = b, arr[cnt][2] = c;
                                                arr[cnt][3] = d, arr[cnt][4] = e, arr[cnt][5] = f;
                                                arr[cnt][6] = g, arr[cnt][7] = h, arr[cnt][8] = i;
                                                arr[cnt][9] = j;
                                                sum++, cnt++;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    printf("%d\n", sum);
    for(int i = 0; i < cnt; i++){
        for(int j = 0; j <= 9; j++){
            printf("%d ", arr[i][j]);
        }
        printf("\n");
    } 
    return 0;
}

有任何问题请站内联系或邮箱zhuo2333@qq.com

原文地址:https://www.cnblogs.com/PineZhuo/p/10460451.html