查找大部分元素
全排列
求幂
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);
}
}