贴几个常用小程序代码,大都递归的

1. 八皇后


public class Test {
    public int []cols=new int[9];
    public int count=0;
    public boolean check(int line,int i){
        int x=i;
        int y=line;
        while(x>0&&y>0){
            if(cols[x]==y)
                return false;
            x--;
            y--;
        }
        x=i;
        y=line;
        while(x>0&&y<9){
            if(cols[x]==y)
                return false;
            x--;
            y++;
        }
        x=i;
        y=line;
        while(x<9&&y>0){
            if(cols[x]==y)
                return false;
            x++;
            y--;
        }
        x=i;
        y=line;
        while(x<9&&y<9){
            if(cols[x]==y)
                return false;
            x++;
            y++;
        }
        return true;
    }
    public void print(){
        count++;
        System.out.println("begin placing...");
        for(int i=1;i<9;i++){            
            for(int j=1;j<9;j++){//每一行
                if(cols[j]==i){
                    System.out.print(" ●");
                }else{
                    System.out.print(" ○");
                }
            }
            System.out.println();
        }
        System.out.println("end placeing...");
    }
    public void place(int line){
        if(line>=9){
            print();
            return;
        }
        for(int i=1;i<9;i++){
            if(cols[i]==0){
                if(check(line,i)){
                    cols[i]=line;
                    place(line+1);
                    cols[i]=0;
                }
            }
        }
    }
    public static void main(String args[]){
        Test test = new Test();
        test.place(1);
        System.out.println("总共有:"+test.count+"个解");
    }
}

2. 排列组合

public class Test2 {
    public int [] data ={1,2,3,4,5,6,7,8,9};
    public int [] result = new int[10];
    public int c(int n,int k){
        if(n<=k||k==0){
            return 1;
        }else{
            return c(n-1,k-1)+c(n-1,k);
        }
    }
    public void A(int[] array,int k,int[]result,int index){
        if(k<=0){
            for(int i=0;i<index;i++){
                System.out.print(result[i]+" ");
            }
            System.out.println();
            return;
        }
        for(int i=0;i<array.length;i++){
            int value = array[i];
            if(value!=-1){
                result[index]=value;
                array[i]=-1;
                A(array,k-1,result,index+1);
                array[i]=value;
            }
        }
    }
    public void C(int[] array,int k,int arrayIndex,int[] result,int index){
        if(k<=0){
            for(int i=0;i<index;i++){
                System.out.print(result[i]+" ");
            }
            System.out.println();
            return;
        }
        if((array.length-arrayIndex-k)<0){
            return;
        }
        for(int i=arrayIndex;i<array.length-k+1;i++){
            int value = array[i];
            if(array[i]!=-1){
                result[index]=value;
                array[i]=-1;
                C(array,k-1,i+1,result,index+1);
                array[i]=value;
            }
        }
        
    }
    
    public static void main(String[] args){
        //System.out.println(new Test2().c(5,3));
        Test2 test2 = new Test2();
        //test2.A(test2.data,2,test2.result,0);
        test2.C(test2.data,2,0,test2.result,0);
    }
}

3. 检查先序是否合法

package com.tuniu;

public class Test3 {
    String[] operators = new String[]{"+","-","*","/"};
    public boolean isOperator(String s){
        for(int i=0;i<operators.length;i++){
            if(s.equals(operators[i])){
                return true;
            }
        }
        return false;
    }
    public int testPre(String str,int scanIndex){
        if(scanIndex>=str.length())
            return -1;
        if(isOperator(str.substring(scanIndex, scanIndex+1))){
            int index = testPre(str,scanIndex+1);
            if(index==-1)
                return -1;
            else{
                return testPre(str,index);
            }
        }else{
            return scanIndex+1;
        }
    }
    public boolean isPre(String str){
        if(testPre(str,0)==str.length())
            return true;
        else
            return false;
    }
    
    public static void main(String[] args){
        String str = "--+ab/ac+bb";
        System.out.println(new Test3().isPre(str));
    }
}

4. 前序中序变后序

public class Test5 {
    //前序定根,中序定左右
    public Node pm2t(String pre,String middle){
        if(pre.length()==0)
            return null;
        Node node = new Node();
        char value = pre.charAt(0);
        node.setValue(value);
        int leftIndex = middle.indexOf(value);
        if(leftIndex!=0){
            node.setLeft(pm2t(pre.substring(1,leftIndex+1),middle.substring(0,leftIndex)));
        }
        if(leftIndex!=middle.length()-1){
            node.setRight(pm2t(pre.substring(leftIndex+1),middle.substring(leftIndex+1)));
        }
        return node;
    }
    public String t2p(Node node,String str){
        if(node.getLeft()!=null)
            str = t2p(node.getLeft(),str);
        if(node.getRight()!=null)
            str = t2p(node.getRight(),str);
        return str+node.getValue();
        
    }
    public void pm2p(String pre,String middle){
        Node node = pm2t(pre,middle);
        System.out.println(t2p(node,""));
    }
    public static void main(String[] args){
        String pre = "stuwv";
        String middle = "uwtvs";        
        new Test5().pm2p(pre,middle);
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值