Written by Bruth_Lee in Southwest University of Science And Technology.
package primer_java;
import java.util.Scanner;
public class Inver {
public static void inver1(int[] a) {
int i,n=a.length;
int[]b = new int[n];
for(i=0;i<n;i++) {
b[n-i-1] = a[i];
}
for(i=0;i<n;i++) {
a[i] = b[i];
System.out.println(a[i]);
}
}
public static void inver2(int[] a) {
int i,n=a.length;
for(i=0 ;i<n/2;i++) {
a[i] = a[i]^a[n-i-1];
a[n-i-1]=a[i]^a[n-i-1];
a[i]=a[i]^a[n-i-1];
}
for(i=0;i<n;i++) {
System.out.println(a[i]);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] a = new int[10];
Scanner in = new Scanner(System.in);
int i;
for(i=0;i<10;i++) {
a[i] = in.nextInt();
}
inver1(a);
inver2(a);
}
}
我们比较就会发现inver2比inver1的算法复杂度要低一些