1 566789 2 656789 3 665789 4 766589 5 866759 6 966785 7 576689 8 586769 9 596786 10 567689 11 568769 12 566987
C++我不会,我用Java写了出来,你看看,自己翻译成C++。
这是算法:
public class Combination{ public static void computer(List<Character> unused, List<Character> used){ if(unused.size()==0){ System.out.println(used); } for(int i=0;i<unused.size(); i++){ List<Character> newUnused=new LinkedList<Character>(unused); List<Character> newUsed=new LinkedList<Character>(used); Character c=newUnused.remove(i); // 该第三个了 if(newUsed.size()==2){ if(c=='8'){ continue; } } // '7'和'9'不能相邻 if(newUsed.size()>0){ char last=newUsed.get(newUsed.size()-1); if((last=='7' && c=='9' )|| (last=='9'&&c=='7')){ continue; } } newUsed.add(c); computer(newUnused, newUsed); } } }
这是测试代码:
List<Character> src=Arrays.asList(new Character[]{'5','6','6','7','8','9'});
List<Character> list=new ArrayList<Character>(src.size());
list.addAll(src);
Combination.computer(list, new ArrayList<Character>(list.size()));
结果:
[5, 6, 6, 7, 8, 9] [5, 6, 6, 9, 8, 7] [5, 6, 7, 6, 8, 9] [5, 6, 7, 6, 9, 8] [5, 6, 7, 8, 6, 9] [5, 6, 7, 8, 9, 6] [5, 6, 9, 6, 7, 8] [5, 6, 9, 6, 8, 7] [5, 6, 9, 8, 6, 7] [5, 6, 9, 8, 7, 6] [5, 6, 6, 7, 8, 9] [5, 6, 6, 9, 8, 7] [5, 6, 7, 6, 8, 9] [5, 6, 7, 6, 9, 8] [5, 6, 7, 8, 6, 9] [5, 6, 7, 8, 9, 6] [5, 6, 9, 6, 7, 8] [5, 6, 9, 6, 8, 7] [5, 6, 9, 8, 6, 7] [5, 6, 9, 8, 7, 6] [5, 7, 6, 6, 8, 9] [5, 7, 6, 6, 9, 8] [5, 7, 6, 8, 6, 9] [5, 7, 6, 8, 9, 6] [5, 7, 6, 9, 6, 8] [5, 7, 6, 9, 8, 6] [5, 7, 6, 6, 8, 9] [5, 7, 6, 6, 9, 8] [5, 7, 6, 8, 6, 9] [5, 7, 6, 8, 9, 6] [5, 7, 6, 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5] [9, 8, 5, 6, 6, 7] [9, 8, 5, 6, 7, 6] [9, 8, 5, 6, 6, 7] [9, 8, 5, 6, 7, 6] [9, 8, 5, 7, 6, 6] [9, 8, 5, 7, 6, 6] [9, 8, 6, 5, 6, 7] [9, 8, 6, 5, 7, 6] [9, 8, 6, 6, 5, 7] [9, 8, 6, 6, 7, 5] [9, 8, 6, 7, 5, 6] [9, 8, 6, 7, 6, 5] [9, 8, 6, 5, 6, 7] [9, 8, 6, 5, 7, 6] [9, 8, 6, 6, 5, 7] [9, 8, 6, 6, 7, 5] [9, 8, 6, 7, 5, 6] [9, 8, 6, 7, 6, 5] [9, 8, 7, 5, 6, 6] [9, 8, 7, 5, 6, 6] [9, 8, 7, 6, 5, 6] [9, 8, 7, 6, 6, 5] [9, 8, 7, 6, 5, 6] [9, 8, 7, 6, 6, 5]
你用的是Java里写好的数据结构和函数,不是我要的答案,零分。如果是用C语言写的,还可以考虑。
@Sam-Po Hau:
我刚看了一下这篇关于C++集合的文章:
http://daizhengxian.blog.163.com/blog/static/301686702010111811379822/
所以你把上面Java代码中的ArrayList,LinkedList换成C++中的list集合。
System.out.println();就是输出数据并换行。
把Java代码中的list.add()换成C++中的push_back()
上述问题就解决了。
我想说的已经很细致到位了。
@螺 丝 钉: 我的意思是不使用C++中的List或者Java中的ArrayList,LinkedList,也不使用相关的push_back()和list.add(),把这些用到的数据结构和操作函数全部自己写出来。
@Sam-Po Hau:
http://blog.csdn.net/hopeyouknow/article/details/6677974
这篇博客应当可以满足你的需求吧
谢谢。
你直接生成 然后用正则去匹配 匹配到的就跳过