新手村 Boss战-入门综合练习1 烤鸡

来源:互联网 发布:杀破狼 js 下载 编辑:程序博客网 时间:2024/05/02 02:12

题目链接

题意理解

这题不就是dfs吗?或者手写10层循环也没有问题啊。注意开的空间大一点就好了

代码

#include <cstring>#include <iostream>#include <cmath>#include <stdio.h>#include <stdlib.h>#include <string>#include <iomanip>#include <vector>using namespace std;int n;int res_cnt = 0;vector<int> res[20000];void read(int &x) {    x = 0;    int f = 1;    char ch = getchar();    while(ch > '9'||ch < '0') {        if(ch == '-') {            f = -1;        }        ch = getchar();    }    while(ch >= '0' && ch <= '9') {        x = x * 10 + (int)(ch - 48);        ch = getchar();    }    x = x * f;}int a[10];void dfs(int cnt) {    if(cnt == 10) {        int sum = 0;        for(int i = 0; i < 10; i++) {            sum += a[i];        }        if(sum == n) {            for(int i = 0; i < 10; i++) {                res[res_cnt].push_back(a[i]);            }            res_cnt++;        }        return;    }    for(int i = 1; i <= 3; i++) {        a[cnt] = i;        dfs(cnt + 1);    }}int main() {    read(n);    if(n < 10) {        printf("0");        return 0;    }    if(n > 30) {        printf("0");        return 0;    }    dfs(0);    printf("%d\n", res_cnt);    for(int i = 0; i < res_cnt; i++) {        for(int j = 0; j < 9; j++) {            printf("%d ", res[i][j]);        }        printf("%d\n", res[i][9]);    }    return 0;}

欢迎加入“不会算法一群菜鸟”,群号是⑥⑥①⑨②2025,这是我设置的一道很低的门槛用来阻止广告的。入群的验证暗号是:我爱编译原理

原创粉丝点击