描述
Ben is writing a small game Sudoku.But he finds it hard to produce a resonable
matrix for a new game.So he asks you for help.
To make the problem easier, we can only generate a line of the matrix.
There are 362880 different kinds of line from 123456789 to 987654321.
In this problem,we regard 123456789 as the first line,regard 123456798
as the second line, and so on.
输入
The input consists of multiple test cases. Each test case consists of an integer n(0<n<=362880). Process to the end of file.
输出
For each test case, print the nth line.
样例输入
1
2
3
362880
样例输出
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 9 8
1 2 3 4 5 6 8 7 9
9 8 7 6 5 4 3 2 1
本题最初我的想法是把输入的值每一位都拆开,这样可以确定每一个值所应对应的值,略微试了一下,此方法太过复杂,可能还会出现各种稀奇古怪的bug,所以果断放弃。
看到邹神代码,活捉一只next_permutation,如法炮制问了度娘,本题就A了,不过时间上惨不忍睹2745MS。
后来把数组改到函数内,提高到2698MS,还是丑的不行。
之后吧do_while改成了for,效率明显上升,到了889MS。
后来突然想到,既然有next_permutation,那为何不可有prev_permutation,二话不说度娘走起,果然又活捉一只新鲜的prev_permutation。
吼吼,时间就提到93MS。
目前还没想到更好的解法,但应该是有的,毕竟有这么多个0MS的解答,看来我的还是略丑,下次回看这题再想一下呗!!!