⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pku1950.cpp

📁 这是ACM 方面的资料 是PKU的 北京大学的出来的
💻 CPP
字号:
#include <stdio.h>

char c[15];
int cnt;

void Out(int d)
{
	int i;
	for (i = 1; i < d; i++)
	{
		printf("%d %c ", i, c[i]);
	}
	printf("%d\n", d);
}

void DFS(int d, int now, int res, int n, int sgn)
{
	if (d == now)
	{
		if (res + n * sgn == 0)
		{
			if (cnt < 20)
			{
				Out(d);
			}
			cnt++;
		}
		return;
	}
	c[now] = '+';
	DFS(d, now + 1, res + n * sgn, now + 1, 1);
	c[now] = '-';
	DFS(d, now + 1, res + n * sgn, now + 1, -1);
	c[now] = '.';
	if (now >= 9)
	{
		DFS(d, now + 1, res, n * 100 + now + 1, sgn);
	}
	else
	{
		DFS(d, now + 1, res, n * 10 + now + 1, sgn);
	}
}

int main()
{
	int N;
	while (scanf("%d", &N) != -1 && N)
	{
		cnt = 0;
		DFS(N, 1, 0, 1, 1);
		printf("%d\n", cnt);
	}
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -