题目链接: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[][]; int main()
{
int n;
scanf("%d", &n);
int x = min(n - , );//每种配料最多只能用3克 if(n <= || n > ){//每种配料的范围在1-3之间小于等于9克或者大于30克均无法完成
printf("");
return ;
} int cnt = , sum = ;
for(int a = ; a <= x; a++){
for(int b = ; b <= x; b++){
for(int c = ; c <= x; c++){
for(int d = ; d <= x; d++){
for(int e = ; e <= x; e++){
for(int f = ; f <= x; f++){
for(int g = ; g <= x; g++){
for(int h = ; h <= x; h++){
for(int i = ; i <= x; i++){
for(int j = ; j <= x; j++){
if(a+b+c+d+e+f+g+h+i+j == n){
arr[cnt][] = a, arr[cnt][] = b, arr[cnt][] = c;
arr[cnt][] = d, arr[cnt][] = e, arr[cnt][] = f;
arr[cnt][] = g, arr[cnt][] = h, arr[cnt][] = i;
arr[cnt][] = j;
sum++, cnt++;
}
}
}
}
}
}
}
}
}
}
}
printf("%d\n", sum);
for(int i = ; i < cnt; i++){
for(int j = ; j <= ; j++){
printf("%d ", arr[i][j]);
}
printf("\n");
}
return ;
}
有任何问题请站内联系或邮箱[email protected]