Algorithm.Induction(归纳法)

查找大部分元素

全排列

求幂

package Algorithm.Induction;
/*Author: CPlusPlus小码农
 *If any question, 
 *Please contact:           
 * http://daixiecplusplus.blog.163.com/
 * QQ:1926742804
 */
public class FindMajorElements {
	public static int candidate(int data[],int i)
	{
		if(i == data.length) return -1;
		int cur = data[i];
		int count =1;
		int j = i;
		while(count > 0 && j < data.length-1)
		{
			j++;
			if(cur == data[j]) count++;
			else count--;
		}
		if( j == data.length-1) return i;
		else return candidate(data,j+1);
	}
	
	public static void major(int data[])
	{
		int c = candidate(data,0);
		if(c != -1){
			int count = 0;
			for(int i = 0; i < data.length ; ++i)
			{
				if(data[c] == data[i])
					count++;
			}
			if(count > data.length/2)
			{
				System.out.println(data[c]);
			}	
			else System.out.println("NULL");
		}
		else System.out.println("NULL");
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int data[] = {1,1,1,4,5};
		FindMajorElements.major(data);
	}

}

package Algorithm.Induction;
/*Author: CPlusPlus小码农
 *If any question, 
 *Please contact:           
 * http://daixiecplusplus.blog.163.com/
 * QQ:1926742804
 */
public class Permutation {
	public static void permu1(StringBuffer sbuf, int m)
	{
		if(m == sbuf.length()) 
			System.out.println(sbuf.toString());
		else
		{
			for(int i = m ; i < sbuf.length(); ++i)
			{
				swap(sbuf,i,m);
				permu1(sbuf,m+1);
				swap(sbuf,m,i);
			}
		}
	}
	
	public static void permu2(int data[],int n,int size)
	{
		if(n == 0)
		{
			for(int i = 0 ; i < size ; ++i)
				System.out.print(data[i]+" ");
			System.out.println();
		}
		else
		{
			for(int i = 0 ; i < size ;++i)
			{
				if(data[i] == 0)
				{
					data[i] = n;
					permu2(data,n-1,size);
					data[i] = 0;
				}
			}
		}
	}
	
	private static void swap(StringBuffer sbuf,int first,int second)
	{
		char temp = sbuf.charAt(first);
		sbuf.setCharAt(first, sbuf.charAt(second));
		sbuf.setCharAt(second, temp);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer("abcde");
		Permutation.permu1(sb, 0);
		
		int data[] = {0,0,0,0,0};
		Permutation.permu2(data,4,4);

	}

}

package Algorithm.Induction;
/*Author: CPlusPlus小码农
 *If any question, 
 *Please contact:           
 * http://daixiecplusplus.blog.163.com/
 * QQ:1926742804
 */
public class Pow {
	public static int pow(int x,int n)
	{
		if( n == 0) return 1;
		else
		{
			int y = pow(x,n/2);
			y = y*y;
			if( n % 2 == 1) y = y*x;
			return y;
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(Pow.pow(5, 3));

	}

}

package Algorithm.Induction;
/*Author: CPlusPlus小码农
 *If any question, 
 *Please contact:           
 * http://daixiecplusplus.blog.163.com/
 * QQ:1926742804
 */
import static org.junit.Assert.*;

import java.util.Random;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class TestPow {
	private static Random random;
	
	@BeforeClass
	public static void Set(){random = new Random();}
	
	@Before
	public void SetUp()
	{
		random = new Random();
		int i = random.nextInt(100);
	}
	
	@Test()
	public void test() {
		//assertEquals(1,2);
		assertEquals("asd",3,3);
		assertEquals("Pow",Pow.pow(2, 2),4);
		assertEquals("Pow",Pow.pow(3, 3),27);
	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值