POJ 1731

#include <stdio.h>
#include <string.h>
#define N_MAX 201
int len;
char buf[201];

void printf_arr()
{
	for (int i = 0; i < len; i++) {
		printf("%c", buf[i]);
	}
	printf("\n");
}

void qsort(char *a, int begin, int end)
{
	if (begin >= end) return;
	int l = begin, r = end, k = a[l];
	while (l != r) {
		while (l < r && k <= a[r]) r--;
		while (l < r && k >= a[l]) l++;
		if (l < r) {
			int tmp = a[l];
			a[l] = a[r];
			a[r] = tmp;
		}
	}
	a[begin] = a[l];
	a[l] = k;
	qsort(a, begin, l - 1);
	qsort(a, l + 1, end);
}

int main(int argc, char* argv[])
{
	memset(buf, 0, sizeof(char) * 201);
	scanf("%s", buf);

	len = strlen(buf);

	qsort(buf, 0, len-1);
	len = strlen(buf);
	printf("%s\n", buf);

	while (1)
	{
			int i = len-1;
			for (; i >= 0; i--) {
				if (buf[i] > buf[i - 1]) {
					break;
				}
			}

			if (i == 0) {
				break;
			}

			int min = 'z';
			int l = i - 1;
			int r = i;
			for (int j = i; j < len; j++) {
				if (buf[j] > buf[l] && buf[j] < min) {
					min = buf[j];
					r = j;
				}
			}

			int tmp = buf[l];
			buf[l] = buf[r];
			buf[r] = tmp;

			qsort(buf, l + 1, len - 1);

			printf_arr();
	}

	return 0;
}

 

posted @ 2017-10-24 13:51  Kjing  阅读(211)  评论(0编辑  收藏  举报