落絮飞雁

顺流而下,把梦做完

POJ3785:The Next Permutation——全排列问题

Description

For this problem, you will write a program that takes a (possibly long) string of decimal digits, and outputs the permutation of those decimal digits that has the next larger value (as a decimal number) than the input number. For example:

123 -> 132
279134399742 -> 279134423799

It is possible that no permutation of the input digits has a larger value. For example, 987.
Input

The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set is a single line that contains the data set number, followed by a space, followed by up to 80 decimal digits which is the input value.
Output

For each data set there is one line of output. If there is no larger permutation of the input digits, the output should be the data set number followed by a single space, followed by the string BIGGEST. If there is a solution, the output should be the data set number, a single space and the next larger permutation of the input digits.
Sample Input

3
1 123
2 279134399742
3 987
Sample Output

1 132
2 279134423799
3 BIGGEST


在STL下瞬间变水题……

输入数组时用scanf()而不用gets()。原因是scanf()遇到空格时认为输入结束,而gets()会输入空格:样例显然是以空格而非 结束标志来判断的。所以,乖乖scanf()吧。

还有就是注意格式,这题又PE了,郁闷……看了看是IDE坏掉了。

放上代码:

#include
#include
#include
char num[1010];
using namespace std;
int main()
{
	int p, n, cache;
	scanf("%d", &p);
	while (p--)
	{
		memset(num, 0, sizeof(num));
		cache = n = 0;
		scanf("%d", &cache);
		scanf("%s", num);
		printf("%d ", cache);
		if (next_permutation(num, num + strlen(num)))
			puts(num);
		else printf("BIGGESTn");
	}
	return 0;
}

 

 


原文标题:POJ3785:The Next Permutation——全排列问题|落絮飞雁的个人网站
原文链接:https://www.luoxufeiyan.com/2015/01/29/poj3785/
授权协议:创作共用 署名-非商业性使用 2.5 中国大陆
除注明外,本站文章均为原创;转载时请保留上述链接。