zoukankan      html  css  js  c++  java
  • Java 超有用算法

    递归全排列

          public static void main(String[] args) {
    		char[] buf = {'a','b','c','d'};
    		perm(buf,0,buf.length-1);
    	}
    	public static void perm(char[] buf,int start ,int end) {
    		// 当读到数组最后一个元素时,遍历数组
    		if(start == end) {
    			for(char c:buf) {
    				System.out.print(c+" ");
    			}
    			System.out.println("");
    		}else {
    			for(int i=start;i<=end;i++) {
    				swap(buf,start,i);
    				perm(buf,start+1,end);
    				swap(buf,start,i);
    			}
    		}
    	}
    	public static void swap(char[] buf,int i,int j) {
    		char temp = buf[i];
    		buf[i] = buf[j];
    		buf[j] = temp;
    	}
    

    最大公约数

          public static int gcd(int p,int q){
                if(q == 0) return p;
                return gcd(q, p % q);
           }
    

    最小公倍数

          public int lcm(int p,int q){
                int pq = p * q;
                return pq / gcd(p,q);
           }
    

    判断素数

      public static boolean fun(int k) {
    	if(k==1||k%2==0&&k!=2) {
    		return false;
    	}else {
    		for(int i = 3;i<=Math.sqrt(k);i++) {
    			if(k%i==0) {
    				return false;
    			}
    		}
    	}
    	return true;
    }
  • 相关阅读:
    暑假学习
    暑假学习
    暑假学习
    暑假学习
    暑假学习
    经验教训+总结
    NT 时刻
    联赛模拟测试 17
    联赛模拟测试 16
    联赛模拟测试 15
  • 原文地址:https://www.cnblogs.com/lyhLive/p/13363579.html
Copyright © 2011-2022 走看看