首页 新闻 会员 周边 捐助

C++面试题求解

0
悬赏园豆:20 [已解决问题] 解决于 2014-08-17 12:19

用5、6、6、7、8、9这六个数字,打印出所有不同的排列,如:569678、675968等,要求'8'不能在第三位,'7'与'9'不能相连。

Sam-Po Hau的主页 Sam-Po Hau | 初学一级 | 园豆:184
提问于:2014-08-16 15:54
< >
分享
最佳答案
0
 1 566789
 2 656789
 3 665789
 4 766589
 5 866759
 6 966785
 7 576689
 8 586769
 9 596786
10 567689
11 568769
12 566987
收获园豆:20
大暗扣 | 菜鸟二级 |园豆:222 | 2014-08-17 01:30
其他回答(2)
0

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);
            }
        }
        
    }
View Code

这是测试代码:

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, 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
[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]
View Code
乐享程序员 | 园豆:930 (小虾三级) | 2014-08-16 20:39

你用的是Java里写好的数据结构和函数,不是我要的答案,零分。如果是用C语言写的,还可以考虑。

支持(0) 反对(0) Sam-Po Hau | 园豆:184 (初学一级) | 2014-08-16 23:30

@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()

上述问题就解决了。

我想说的已经很细致到位了。

支持(0) 反对(0) 乐享程序员 | 园豆:930 (小虾三级) | 2014-08-17 03:38

@螺 丝 钉: 我的意思是不使用C++中的List或者Java中的ArrayList,LinkedList,也不使用相关的push_back()和list.add(),把这些用到的数据结构和操作函数全部自己写出来。

支持(0) 反对(0) Sam-Po Hau | 园豆:184 (初学一级) | 2014-08-17 10:57

@Sam-Po Hau: 

http://blog.csdn.net/hopeyouknow/article/details/6677974

这篇博客应当可以满足你的需求吧

支持(0) 反对(0) 乐享程序员 | 园豆:930 (小虾三级) | 2014-08-17 11:10

谢谢。

支持(0) 反对(0) Sam-Po Hau | 园豆:184 (初学一级) | 2014-08-17 12:19
0

你直接生成 然后用正则去匹配 匹配到的就跳过

望着天的蜗牛 | 园豆:354 (菜鸟二级) | 2014-08-17 21:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册