待删
题目来源:洛谷 / 题目列表 / 题目详情
P2089 烤鸡
详情:
猪猪 Hanke 特别喜欢吃烤鸡(本是同畜牲,相煎何太急!)Hanke 吃鸡很特别,为什么特别呢?因为他有 10 种配料(芥末、孜然等),每种配料可以放 1到 3克,任意烤鸡的美味程度为所有配料质量之和。
现在, Hanke 想要知道,如果给你一个美味程度 n,请输出这 10 种配料的所有搭配方案。
分析:
由配料范围1至3克,可知道该题有“暴力枚举”的方向。可以用暴力枚举解出。
代码如下:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,sum=0;
cin>>n;
if(n>=10&&n<=30){
for(int a=1;a<=3;a++){
for(int b=1;b<=3;b++){
for(int c=1;c<=3;c++){
for(int d=1;d<=3;d++){
for(int e=1;e<=3;e++){
for(int f=1;f<=3;f++){
for(int i=1;i<=3;i++){
for(int g=1;g<=3;g++){
for(int h=1;h<=3;h++){
for(int j=1;j<=3;j++){
if(a+b+c+d+e+f+g+h+i+j==n) sum++;
}
}
}
}
}
}
}
}
}
}
cout<<sum<<endl;
for(int a=1;a<=3;a++){
for(int b=1;b<=3;b++){
for(int c=1;c<=3;c++){
for(int d=1;d<=3;d++){
for(int e=1;e<=3;e++){
for(int f=1;f<=3;f++){
for(int i=1;i<=3;i++){
for(int g=1;g<=3;g++){
for(int h=1;h<=3;h++){
for(int j=1;j<=3;j++){
if(a+b+c+d+e+f+g+h+i+j==n) cout<<a<<" "<<b<<" "<<c<<" "<<d<<" "<<e<<" "<<f<<" "<<i<<" "<<g<<" "<<h<<" "<<j<<endl;
}
}
}
}
}
}
}
}
}
}
}else cout<<"0";
return 0;
}
事实上,如果题目的数据范围出现过小,都应该考虑一下使用暴力枚举的方向!
代码如上,欢迎讨论。