我想做做一个excel表格 实现词语的排列组合
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我想做做一个excel表格 实现词语的排列组合相关的知识,希望对你有一定的参考价值。
比如 从1--10 , 十个数中任取2个组合 然后输出组合的结果
从n个不同元素中,任取m(3≤n)个元素并成一组, 要求是输出所有的组合可能
希望能做出一个excel模板发给我 , 邮箱 915698296@qq.com
下面是在网上找的一些资料 但是我还是做不出来
http://zhidao.baidu.com/question/110852222.html
http://club.excelhome.net/thread-15309-2-1.html
Sub PL()
zn = 2
Dim sz(0 To 99) As String
znagain:
If Cells(zn, 1) = "" Then GoTo znend
sz(zn - 1) = Cells(zn, 1)
zn = zn + 1: GoTo znagain
znend:
zn = zn - 2 '获得数组项数
tm = Cells(2, 2)
m = 2
Range("c2:c65535").ClearContents
For pm = m To m + zn '初始第一组排列
Cells(pm, 3) = sz(pm - 1)
Next pm
fms = m '初始排列循环起点
fme = m + zn - 1 '初始排列循环终点
m = m + zn + 1
For nm = 2 To tm '项数循环
For pol = fms To fme
For pm = 1 To zn
If m = 65535 Then MsgBox "超出65535行": GoTo fend
Cells(m, 3) = Cells(pol, 3) & "," & sz(pm)
m = m + 1 '排列行累加
Next pm
Next pol
fms = fme + 2 '排列循环起点
fme = m - 1 '排列循环终点
m = m + 1 '新项数跳行
Next nm
MsgBox "完成"
fend:
End Sub
以上是含重复项的,不含重复项的代码如下:
Sub PL()
zn = 2
Dim sz(0 To 99) As String
znagain:
If Cells(zn, 1) = "" Then GoTo znend
sz(zn - 1) = Cells(zn, 1)
zn = zn + 1: GoTo znagain
znend:
zn = zn - 2 '获得数组项数
tm = Cells(2, 2)
m = 2
Range("c2:c65535").ClearContents
For pm = m To m + zn '初始第一组排列
Cells(pm, 3) = sz(pm - 1)
Next pm
fms = m '初始排列循环起点
fme = m + zn - 1 '初始排列循环终点
m = m + zn + 1
For nm = 2 To tm '项数循环
sofm = 0
For pol = fms To fme
For pm = 1 To zn
sopm = Cells(pol, 3) & "," & sz(pm)
For cks = 1 To Len(sopm) Step 2
If InStr(cks + 1, sopm, Mid(sopm, cks, 1)) > 0 Then GoTo nextpm
Next cks
Cells(m, 3) = sopm
If m = 65535 Then MsgBox "超出65535行": GoTo fend
m = m + 1 '排列行累加
nextpm:
Next pm
Next pol
fms = fme + 2 '排列循环起点
fme = m - 1 '排列循环终点
m = m + 1 '新项数跳行
Next nm
MsgBox "完成"
fend:
End Sub
如需沟通解释,可以Q644689736 参考技术A 这个不太现实,需要的判断条件太多、单元格复制粘贴也没有什么规律,做起来会很麻烦。
如果仅仅是词语的排列,可以直接使用Excel的排序功能。 参考技术B 枚举不定元素问题,用数据表没法实现(复杂程序太高)。
最好的方法是用循环程序。
用任意程序语言都可以简单实现 参考技术C 先把数据一边对齐,比如说左对齐,然后点击数据-分列-固定列宽,用分列线把前两位分开,下一步下一步就可以了,这样就把数据和**分成两列了,下边就不用教了吧?哈哈,不过要注意,因为分列后原来的一列会变成两列,所以会填充掉后边的一列,如果后边一列还有数据,不希望被填掉,应该在分列前在备分的列后插入一列空列,这样就可以避免有用的数据丢失掉了
另外,站长团上有产品团购,便宜有保证 参考技术D 看看你的文件,最好写明详细要求。HI我。追问
现在写详细了 能来帮下忙吗 谢谢
追答加Q:85161959,把文件传给我看看。
java实现排列组合(通俗易懂)
个人感觉这篇文章(原文地址见文章尾)写的排列组合问题,非常的好,而且是一步一步引出排列组合问题,我也是看了这篇文章,一步一步按照这个思路来,最后会了自己的一套排列组合
也因此在算法竞赛中,两次用到了,成功解决了问题.
第一个问题:
首先,先让我们来看第一个问题, 有1,2,3,4这4个数字.可以重复的在里面选4次,问能得到多少种结果.easy
1 1 1 1
1 1 1 2
1 1 1 3
1 1 1 4
1 1 2 1
1 1 2 2
.......
4 4 4 3
4 4 4 4
代码实现其实也很简单,大家可以看下代码,理解一下,再自己敲一下,应该可以很快敲出来
import java.util.Stack; public class Main { public static Stack<Integer> stack = new Stack<Integer>(); public static void main(String[] args) { int shu[] = {1,2,3,4}; f(shu,4,0); } /** * * @param shu 待选择的数组 * @param targ 要选择多少个次 * @param cur 当前选择的是第几次 */ private static void f(int[] shu, int targ, int cur) { // TODO Auto-generated method stub if(cur == targ) { System.out.println(stack); return; } for(int i=0;i<shu.length;i++) { stack.add(shu[i]); f(shu, targ, cur+1); stack.pop(); } } }
输出:
[1, 1, 1, 1] [1, 1, 1, 2] [1, 1, 1, 3] [1, 1, 1, 4] [1, 1, 2, 1] [1, 1, 2, 2] [1, 1, 2, 3] [1, 1, 2, 4] [1, 1, 3, 1] [1, 1, 3, 2] [1, 1, 3, 3] [1, 1, 3, 4] [1, 1, 4, 1] [1, 1, 4, 2] [1, 1, 4, 3] [1, 1, 4, 4] [1, 2, 1, 1] [1, 2, 1, 2] [1, 2, 1, 3] [1, 2, 1, 4] [1, 2, 2, 1] [1, 2, 2, 2] [1, 2, 2, 3] [1, 2, 2, 4] [1, 2, 3, 1] [1, 2, 3, 2] [1, 2, 3, 3] [1, 2, 3, 4] [1, 2, 4, 1] [1, 2, 4, 2] [1, 2, 4, 3] [1, 2, 4, 4] [1, 3, 1, 1] [1, 3, 1, 2] [1, 3, 1, 3] [1, 3, 1, 4] [1, 3, 2, 1] [1, 3, 2, 2] [1, 3, 2, 3] [1, 3, 2, 4] [1, 3, 3, 1] [1, 3, 3, 2] [1, 3, 3, 3] [1, 3, 3, 4] [1, 3, 4, 1] [1, 3, 4, 2] [1, 3, 4, 3] [1, 3, 4, 4] [1, 4, 1, 1] [1, 4, 1, 2] [1, 4, 1, 3] [1, 4, 1, 4] [1, 4, 2, 1] [1, 4, 2, 2] [1, 4, 2, 3] [1, 4, 2, 4] [1, 4, 3, 1] [1, 4, 3, 2] [1, 4, 3, 3] [1, 4, 3, 4] [1, 4, 4, 1] [1, 4, 4, 2] [1, 4, 4, 3] [1, 4, 4, 4] [2, 1, 1, 1] [2, 1, 1, 2] [2, 1, 1, 3] [2, 1, 1, 4] [2, 1, 2, 1] [2, 1, 2, 2] [2, 1, 2, 3] [2, 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[4, 4, 1, 3] [4, 4, 1, 4] [4, 4, 2, 1] [4, 4, 2, 2] [4, 4, 2, 3] [4, 4, 2, 4] [4, 4, 3, 1] [4, 4, 3, 2] [4, 4, 3, 3] [4, 4, 3, 4] [4, 4, 4, 1] [4, 4, 4, 2] [4, 4, 4, 3] [4, 4, 4, 4]
第二个问题:
同理, 问题来了,这时候有点排列组合的意思了,1,2,3,4排列要的到的是
1 2 3 4
1 2 4 3
1 3 4 2
1 3 2 4
......
4 2 1 2
4 3 2 1
有没有发现要的到排列的情况,这里stack里的元素是1,2,3,4都不能重复
那么我在入栈的时候加个判断,如果比如1,已经在stack里面了,就不加进去,就不会得到 1 1 1 1 ...的情况了,就得到了排列
import java.util.Stack; public class Main { public static Stack<Integer> stack = new Stack<Integer>(); public static void main(String[] args) { int shu[] = {1,2,3,4}; f(shu,4,0); } /** * * @param shu 待选择的数组 * @param targ 要选择多少个次 * @param cur 当前选择的是第几次 */ private static void f(int[] shu, int targ, int cur) { // TODO Auto-generated method stub if(cur == targ) { System.out.println(stack); return; } for(int i=0;i<shu.length;i++) { if(!stack.contains(shu[i])) { stack.add(shu[i]); f(shu, targ, cur+1); stack.pop(); } } } }
输出:
[1, 2, 3, 4] [1, 2, 4, 3] [1, 3, 2, 4] [1, 3, 4, 2] [1, 4, 2, 3] [1, 4, 3, 2] [2, 1, 3, 4] [2, 1, 4, 3] [2, 3, 1, 4] [2, 3, 4, 1] [2, 4, 1, 3] [2, 4, 3, 1] [3, 1, 2, 4] [3, 1, 4, 2] [3, 2, 1, 4] [3, 2, 4, 1] [3, 4, 1, 2] [3, 4, 2, 1] [4, 1, 2, 3] [4, 1, 3, 2] [4, 2, 1, 3] [4, 2, 3, 1] [4, 3, 1, 2] [4, 3, 2, 1]
这就是想要的排列结果了..
第三个问题:
明天待续....
原文地址: https://blog.csdn.net/Ring_k/article/details/79575533
以上是关于我想做做一个excel表格 实现词语的排列组合的主要内容,如果未能解决你的问题,请参考以下文章
两个Excel有一列内容相同,这一列排列顺序不一样,怎么把这两个表内容合并成一张表?
在excel中,如果将若干单元格里面的字符串,在后面的单元格输出所有的排列或者组合,且包括所有的字符串