Matlab 【全排列与对换 逆序数】

 1 clc
 2 clear
 3 %author:正义V领
 4 %date:2018-10-12
 5 x=input('请输入数据');%输入数据
 6 str=num2str(x);%转换字符串
 7 for i=1:length(str)%字符串长度
 8     r(i)=str2double(str(i));%再转换为双精度
 9 end
10 if length(r)==1 %如果长度为1则逆序数为0
11     disp('逆序数为');
12     disp(0);
13 else
14      for j=2:length(r)
15           s=find(r(1:j-1)>r(j))%查找逆序数的下标索引
16           u(j)=length(s)%生成每个下标索引长度组成的数组u
17          result=sum(u)%求和
18      end
19      disp('逆序数为');
20      disp(result);
21 end
逆序数

 

posted @ 2018-10-12 21:08  Justice-V  阅读(777)  评论(0)    收藏  举报