各位小伙伴们,过来看一看吧,一道简单的C++编程题,题目描述如下,谢谢!

输入数字n和m,按字典序输出从1-n这n个自然数中取m个数排列构成的所有数字。
输入样例
4
2
输出样例
1 2
1 3
1 4
2 1
2 2
2 3
2 4
3 1
3 2
3 3
3 4

你的输出样例真的没问题?。。

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int a[100],c[100];
void dfs(int n,int m,int t)
{
    if(t==m)
    {
        for(int i=0;i<m;i++)
        cout<<a[i]<<" ";
        cout<<endl;
        return ;
    }
    for(int i=1;i<=n;i++)
    {
        if(c[i]!=0)continue;
        c[i]++;
        a[t]=i;
        dfs(n,m,t+1);
        c[i]--;
    }
}
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        memset(c,0,sizeof(c));
        dfs(n,m,0);
    }
    return 0;
}

追问

应该不会有错吧,不过还是采纳了吧

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答