P2089 烤鸡(dfs)

传送门

题解:dfs进行递归搜索,感觉考CCF前看了这道题,完全可以上300啊,加油,事情过去了不可怕,可怕的是从事情中一无所获。

附上代码:


#include<bits/stdc++.h>

using namespace std;

const int maxn=60000;

int res[maxn][10],temp[10],n,kind;

void peiliao(int total,int pos)
{
    if(pos==10){
        if(total==n){
            for(int i=0;i<10;i++){
                res[kind][i]=temp[i];
            }
            kind++;
        }
    }
    else if(total>n);
    else{
        for(int i=1;i<=3;i++){
            temp[pos]=i;
            peiliao(total+i,pos+1);
        }
    }
}

int main()
{
    cin>>n;
    peiliao(0,0);
    cout<<kind<<endl;
    for(int i=0;i<kind;i++){
        for(int j=0;j<10;j++){
            cout<<res[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/zhouzi2018/article/details/82822389
今日推荐