#P0867. 小武老师的烤全羊

问题描述
小武老师特别喜欢吃烤全羊,小武老师吃烤全羊很特别,为什么特别呢?因为他有 1010 种配料(芥末、孜然等),每种配料可以放 11 到 33 克,任意烤全羊的美味程度为所有配料质量之和。

现在, 小武老师想要知道,如果给你一个美味程度 nn ,请输出这 1010 种配料的所有搭配方案。

输入格式
一个正整数 nn ,表示美味程度。

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

第二行至结束,1010 个数,表示每种配料所放的质量,按字典序排列。

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

输入数据 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
数据范围与约定
对于 100%100% 的数据,n \leq 5000n≤5000 。

代码:

#include<bits/stdc++.h>
using namespace std;
int n, cnt, a[10000][10], b[1001];
void dfs(int step, int sum) {
    
      
	int i;
	if (step == 10) {
    
      
		if (sum == n) {
    
    
			for (i = 0; i < 10; i++)a[cnt][i] = b[i];     
			cnt++;      
		}
		return;
	}
	for (i = 1; i < 4; i++) {
    
       
		b[step] = i;           
		dfs(step + 1, sum + i);       
	}
}
int main() {
    
    
	int i, j;
	cin >> n;
	dfs(0, 0);
	if (!cnt)cout << "0";
	else {
    
      
		cout << cnt << endl;  
		for (i = 0; i < cnt; i++) {
    
              
			for (j = 0; j < 10; j++)cout << a[i][j] << " ";
			cout << endl;            
		}
	}
    return 0;
}

猜你喜欢

转载自blog.csdn.net/noiqqq123456/article/details/132475651