标签:des style blog color os java ar strong for
public static void main(String[] args) { int[] arr = {5,8,9,12,55,565,421,12,2512,-5,-56}; sortMethod(arr); p(arr); } //排序核心代码 private static void sortMethod(int arr[]) { for(int i = 0 ;i<arr.length - 1;i++){ for(int j = i + 1;j<arr.length;j++){ if(arr[i] > arr[j]){ int temp = arr[j]; arr[j] = arr[i]; arr[i] = temp; } } } } //打印数组元素 static void p(int[] arr) { String ret = "["; for(int i = 0;i<arr.length;i++){ ret += arr[i]; if(i == arr.length - 1){ ret += "]"; }else{ ret += ","; } } System.out.println("---------->"+ret);
public static void main(String[] args) { int arr[] = {6,9,4589,442,458,5232,-788,7,545,-44,55,-11}; sortMethod(arr); p(arr); } private static void p(int[] arr) { String ret = "["; for(int i = 0; i < arr.length; i ++){ ret += arr[i]; if( i == arr.length - 1){ ret += "]"; }else{ ret += ","; } } System.out.println("---------->" + ret); } private static void sortMethod(int[] arr) { for(int j = 1;j<arr.length - 1;j++){ for(int i = 0; i <arr.length - j;i++){ if(arr[i] > arr[i + 1]){ int temp = arr[i]; arr[i] = arr[i + 1]; arr[i + 1] = temp; } } } }
1 //查找指定数组指定元素的索引值。 2 public static void main(String[] args) { 3 int[] arr = {5,4,231,45,75,45,11,-11,-21,-45}; 4 int key = 45; 5 sortMethod(arr); 6 printSort(arr); 7 int index = binarySeach(arr,key,0,arr.length - 1); 8 System.out.println("----------->所求索引值:"+index); 9 } 10 //二分查找法核心代码 11 private static int binarySeach(int[] arr, int key,int fromIndex,int toInedx) { 12 //最小索引与最大索引 13 //fromIndex 表示从哪个位置开始查找,toIndex表示到那个位置结束 14 int minIndex = fromIndex,maxIndex = toInedx; 15 while(maxIndex >= minIndex){ 16 //中间索引 17 int midIndex = (maxIndex + minIndex)/2; 18 //中间索引的值 19 int midIndexVal = arr[midIndex]; 20 if(key > midIndexVal){ 21 minIndex = midIndex + 1; 22 }else if(key < midIndexVal){ 23 maxIndex = midIndex - 1; 24 }else{ 25 return midIndex; 26 } 27 } 28 return -1; 29 } 30 //打印排序后的代码 31 private static void printSort(int[] arr) { 32 String ret = "["; 33 for(int i = 0;i<arr.length;i++){ 34 ret +=arr[i]; 35 if(i == arr.length - 1){ 36 ret += "]"; 37 }else{ 38 ret += ","; 39 } 40 } 41 System.out.println("----------->排序:" + ret); 42 } 43 //对代码进行排序 44 private static void sortMethod(int[] arr) { 45 for(int j = 0;j<arr.length - 1;j++){ 46 for(int i = 0;i<arr.length - j - 1;i++){ 47 if(arr[i] > arr[i + 1]){ 48 int temp = arr[i]; 49 arr[i] = arr[i+1]; 50 arr[i+1] = temp; 51 } 52 } 53 } 54 }
数组的拷贝:
1 /** 2 * 3 * @author Essence 4 *arraycopy(Object src, int srcPos, Object dest,int destPos, int length) 5 *该方法接收四个参数 6 *object src :原数组,即被拷贝的数组 7 *int srcPos: 原数组的目标位置 即从哪个位置开始拷贝起 8 *Object dest:目标数组 即用来存储被拷贝的数组 9 *int destPos:目标数组中的起始位置,即向目标数组中插入数据的起始位置 10 *int length:要复制的元素数量 11 */ 12 public static void main(String[] args) { 13 String[] arr = {"A","B","C","D"}; 14 String[] arr1 = new String[5]; 15 System.out.println(Arrays.toString(arr1)); // 拷贝之前 : [null, null, null, null, null] 16 System.arraycopy(arr, 1, arr1,2, 3); 17 System.out.println(Arrays.toString(arr1)); //拷贝之后: [null, null, B, C, D] 18 19 }
可变参数:
1 /** 2 * Java5 出现的新特性 参数可变 3 * 可变参数必须作为参数的最后一个参数 ,参数可以直接传递0到n个 4 *其本质上就是数组 5 * method(int ... arr) 6 * @author Essence 7 * 8 */ 9 public static void main(String[] args) { 10 int[] arr = {1,2,347,7,78,9}; 11 System.out.println(getSum(arr)); 12 /** 13 * 可变参数调用特点: 14 * getSum(1,2,34,5) 15 */ 16 System.out.println(getSum(1,2,34,5)); 17 18 } 19 private static int getSum(int ... arr) { 20 int sum = 0; 21 for(int i = 0;i<arr.length;i++){ 22 sum += arr[i]; 23 } 24 return sum; 25 } 26 }
1 public class Demo{ 2 public static void main(String[] args) { 3 long sum = factorial(10); 4 System.out.println(sum); 5 } 6 static long factorial(int n){ 7 if(n==1){ 8 return 1; 9 }else{ 10 return n*factorial(n-1); 11 } 12 } 13 }
Java编程思想上的两个题斐波那契数列与吸血鬼数字
斐波那契数列:
/*
* 1,1,2,3,5,8,13,21,34
* 起那两个数是第三个数的和
* 斐波那契数列
* F(n) = F(n-1)+F(n-2)
*/
方法一: private static void fibonacci(int n) { int arr[] = new int[n],sum = 0; arr[0] = arr[1] = 1; for(int i = 2;i<arr.length;i++){ arr[i] = arr[i - 1] + arr[i-2]; System.out.println("arr["+i+"] "+arr[i]); sum += arr[i]; } System.out.println("斐波那契数列之和:" +sum); } 方法二: private static int sumFibonacci(int n) { if(n<1){ return 1; }else{ return sumFibonacci(n - 1) + sumFibonacci(n - 2); } } private static void getFibonacci(int n) { for(int i = 0;i <= n;i++){ int f = sumFibonacci(i); System.out.print(f + "\t"); if(i%3 == 0){ System.out.println(); } } } 方法三: private static void fibbonacci1(int n){ int a = 1,b = 1,c = 0 , sum = 0; System.out.println(a+"\t"+b+"\t"); for(int i = 1;i<=n;i++){ c =a +b; a = b; b = c; sum += c; System.out.print(c+"\t"); if(i%3==0){ System.out.println(); } } System.out.println("斐波那契数列之和:" +sum); }
吸血鬼数字:
/*
*1260=21*60
*1827=21*87
*2187=27*81
*/
1 private static void vampireNumber1() { 2 for(int i = 1;i<100;i++){ 3 for(int j = 1;j<100;j++){ 4 if(i*j>1000){ 5 String a = i+""+j; 6 String b = i*j+""; 7 if(equals(a,b)){ 8 System.out.println(i+"\t"+j+"\t"+i*j); 9 } 10 } 11 } 12 } 13 } 14 private static boolean equals(String a, String b) { 15 // TODO Auto-generated method stub 16 char[] aArrays ,bArrays; 17 aArrays = a.toCharArray(); 18 bArrays = b.toCharArray(); 19 Arrays.sort(aArrays); 20 Arrays.sort(bArrays); 21 if(Arrays.equals(aArrays,bArrays)){ 22 return true; 23 } 24 return false; 25 }
标签:des style blog color os java ar strong for
原文地址:http://www.cnblogs.com/Essence/p/3961857.html