HDU - 1716 : http://acm.hdu.edu.cn/showproblem.php?pid=1716

Problem Description

Input

Output

Sample Input

Sample Output

#include<iostream>
#include<set>
#include<algorithm>
using namespace std;

int card[4];
int vis[4];
set<int> ans;

void print_ans() {    //注意每一行结尾没有空格

	set<int>::iterator it = ans.begin();
	int tag = *it;
	cout << tag;
	it++;

	for (; it != ans.end(); it++) {
		if (*it / 1000 == tag / 1000) {
			cout << " ";
		}
		else {
			cout << endl;
		}
		tag = *it;
		cout << tag;
	}
}

void DFS(int *a, int k) {
	if (k == 4) {
		if (a[0] != 0) {
			ans.insert(a[0] * 1000 + a[1] * 100 + a[2] * 10 + a[3]);
		}
		return;
	}
	for (int i = 0; i < 4; i++) {
		if (!vis[i]) {
			vis[i] = 1;
			a[k] = card[i];
			DFS(a, k + 1);
			vis[i] = 0;
		}
	}
}

int main() {

	cin >> card[0] >> card[1] >> card[2] >> card[3];
	while (!(card[0] == 0 && card[1] == 0 && card[2] == 0 && card[3] == 0)) {

		int a[4];
		ans.clear();
		memset(vis, 0, sizeof(vis));

		DFS(a, 0);
		print_ans();
		cout << endl;

		cin >> card[0] >> card[1] >> card[2] >> card[3];
		if (!(card[0] == 0 && card[1] == 0 && card[2] == 0 && card[3] == 0))cout << endl;
	}

	return 0;
}

 

10-04 16:41