排列的序号Time Limit:1000MS Memory Limit:65536K
Total Submit:28 Accepted:6 Description 如果我们将n个元素的全排列象字符串一样按字典排序的话,则其中的每一个排列都对应唯一的一个序号。如n=4时,排列1 2 3 4 的序号为0,而排列 4 3 2 1的序号为23。要求在给定n(n为自然数,由键盘输入)后,编程实现:输入一个1到n的排列,输出其序号。
Input 两行数
第一行:n(排列中数的个数) (n<=10)
第二行:某个排列(空格隔开)
Output 一个整数(表示这个排列的序号)(序号在长整范围内)
Sample Input
Sample Output
23我的程序:var m,n,i,j,x,k:longint;a,b:array[1..20] of integer;procedure print;begin for i:=1 to n-1 dofor j:=i to n doif (ij) and (a=a) then exit; inc(k); for i:=1 to n doif ab then exit; writeln(k); haltend;procedure change(ws:integer); beginif ws=n then begin repeatinc(a);if (a=m; endelse repeatinc(a);if (ws1) and (a=m; end;begin k:=-1; readln(n); for i:=1 to n doread(b); m:=n; for i:=1 to n doa:=i-1; change(1);end.
第n个排列Time Limit:1000MS Memory Limit:65536K
Total Submit:9 Accepted:5 Description 如果我们将n个元素的全排列象字符串一样按字典排序的话,则其中的每一个排列都对应唯一的一个序号。如n=4时,排列1 2 3 4 的序号为0,而排列 4 3 2 1的序号为23。要求在给定n(n为自然数,由键盘输入)后,编程实现:输入一个排列的序号,输出相应的排列。
Input 两行整数
第一行 n(排列中数的个数)(n<=10)
第二行 一个整数(长整范围内一个数)
Output 一行整数(表示所求的排列,每个数之间一个空格隔开)
Sample Input
Sample Output
1 2 3 4
我的程序:var m,n,i,j,x,k:longint;a:array[1..20] of integer;procedure print;begin for i:=1 to n-1 dofor j:=i to n doif (ij) and (a=a) then exit; inc(k); if