Java Classic Small algorithm

Source: Internet
Author: User

Package Com.shb.java;public class Demo4 {/** time limited do not write the text of their own random knock * @param args * @author shaobn */public static void Main (St Ring[] (args) {//TODO auto-generated method Stub//system.out.println (Getdir);//getmethod (New int[]{ 0,15,23,455,45,12}); Getmethod_2 (new int[]{1,45,848,66});} Up stairs problem public static int Getdir (int n) {if (n==1) {return 1;} else if (n==2) {return 2;} else if (n>2) {return Getdir (n-1) +getdir (n-2);} return 1;} Move the array forward, the last one for the original array, the first public static void GetMethod (int[] array) {int temp =0;int cur = 0;for (int i=0;i< array.length;i++) {if (i==0) {temp = array[i];continue;} Array[cur] = array[i];cur++;if (i==array.length-1) {array[i] = temp;}} for (int i:array) {System.out.println (i);}} Reverse array output public static void Getmethod_2 (int[] array) {if (array.length%2==0) {int tmp = 0;for (int i=0;i<array.length/2 ; i++) {tmp=array[i];array[i] = array[array.length-i-1];array[array.length-i-1]=tmp;} for (int i:array) {System.out.println (i);}} else {int tmp = 0;for (int i=0;i< (array.length-1)/2;i++) {TMp = array[i];array[i] = array[array.length-i-1];array[array.length-i-1]=tmp;} for (int i:array) {System.out.println (i);}}}}

Java Classic Small algorithm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.