วันพฤหัสบดีที่ 17 กันยายน พ.ศ. 2552

Swap

import java.util.Scanner;


public class Swap {

public static void main(String args[])

{

Scanner scan=new Scanner(System.in);

int amount=scan.nextInt();



int num[]=new int [amount];

//////////get value from keyboard

for(int i=0; i < num.length ; i++)

{

num[i]=scan.nextInt();

}

//////////////////////////////





/////Swap

int temp;

int i3=amount-1;

for(int i2=0; i2 < num.length/2 ; i2++)

{

temp=num[i2];

num[i2]=num[i3];

num[i3]=temp;

i3--;

}

//////////////////////////////////



//example amount=5;

///int num[]=new num[amount];

// array have 0 1 2 3 4

//give i3=5-1;





///i2 < num.length/2  because for once can swap 2 value

//in this case num.length/2 = 2;

//so , it will for 2 times



//first for i3=4;

//i2=0;



////second for i3=3;

//i2=1;



////i2=2; it will be not change value



///test input 1 2 3 4 5



//first

//1=5

//5=1



//second

//2=4

//4=2



//3 not swap because 3 is mid value

//it will be not change value

////////////////////////////////////









///////////////show value

for(int i4=0; i4 < num.length ; i4++)

{

System.out.printf("%d\n",num[i4]);

}



}

}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น