关于如何删除程序输出的最后的空行
查看原帖
关于如何删除程序输出的最后的空行
551088
FincheuwYggdrasil楼主2022/9/11 21:13

rt

输入数据:

6
8

代码:

/*
* 题目:hdu1016
* 来源:acm.hdu.edu.cn 
* 时间:22.9.11
* 算法:DFS 回溯 
*/
#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int ans[30];
bool vis[30],isfirst = true;
int q,rnd = 0;
bool prime(int k)
{
	for(int i = 2;i * i <= k;i++)
		if(!(k % i))
			return false;
	return true;
}
void dfs(int depth)
{
	if(depth == q + 1 && prime(ans[1] + ans[q]))
	{
		for(int i = 1;i < q;i++)
			printf("%d ",ans[i]);
		printf("%d\n",ans[q]);
		return;
	}
	for(int i = 2;i <= q;i++)
	{
		if(!vis[i] && prime(i + ans[depth - 1]))
		{
			ans[depth++] = i;
			vis[i] = true;
			dfs(depth);
			vis[i] = false;
			depth--;
		}
	}
	return ;
}
int main()
{
	freopen("xxx.in","r",stdin);
	freopen("xxx.out","w",stdout);
	while(~scanf("%d",&q))
	{
		if(isfirst)
			isfirst = false;
		else
			printf("\n");
		printf("Case %d:\n",++rnd);
		memset(vis,0,sizeof(vis));
		memset(ans,0,sizeof(ans));
		ans[1] = 1;
		vis[1] = true;
		dfs(2);
		/*printf("\n");*/
 	}
	return 0;
}

2022/9/11 21:13
加载中...