站外题求助
  • 板块学术版
  • 楼主Fist_PROF
  • 当前回复44
  • 已保存回复44
  • 发布时间2022/8/11 11:28
  • 上次更新2023/10/27 15:59:17
查看原帖
站外题求助
446093
Fist_PROF楼主2022/8/11 11:28

题面:

第7题     组合3 查看测评数据信息
 从前N个正整数中取M个的所有组合,但要求所有数互质。按照字典序从小到大输出。

输入格式
一行2个正整数N和M,  0< M <= N <10

输出格式
每行一个排列。

输入/输出例子1
输入:
6  3
输出:
1 2 3
1 2 5
1 3 4
1 3 5
1 4 5
1 5 6
2 3 5
3 4 5
#include<bits/stdc++.h>
using namespace std;
int ans[20];
int N,M;
int Aqours(int a,int b){
	if(b==0)return a;
	return Aqours(b,a%b);
}
int Liella(int a,int b){
	int c=Aqours(a,b);
	if(c>1)return 0;
	return 1;
}
void out() {
	for(int i=1;i<=N;i++)
	    if (ans[i]&&Liella(ans[i],ans[i+1]))
	        cout<<i<<" ";
	cout<<endl;
}
void com(int step,int c) {
	if(c>M) return ;
	if(c+(N-step+1)<M) return ;
	if(step>N) {
		out();
		return ;
	}
	ans[step]=1;
	com(step+1,c+1);
	ans[step]=0;
	com(step+1,c);
}
int main() {
	cin>>N>>M;
	ans[0]=-1;
	com(1,0);
	return 0;
}

我的代码有很大问题,输出些奇怪的东西

希望大佬能帮忙看一下,谢谢

2022/8/11 11:28
加载中...