Reverse-EVEN ROWS AND ODD COLUMNS
input:3 4
96 53 51 15
23 11 72 68
77 53 74 47
output:
77 53 74 15
68 72 11 23
96 53 51 47
I want program in java
Answers
Answer:
FOR JAVA
Explanation:
/*
Enter some numbers to fill the list.
Input: 2 6 9
Output: 9 6 2
*/
import java.util.ArrayList;
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<Integer> myList = new ArrayList<>();
while(scan.hasNext()){
try{
myList.add(scan.nextInt());
}catch(Exception e){
System.out.println("Enter only numbers.");
return;
}
}
System.out.println("original: " + myList);
if(myList.size() <= 1){
System.out.println("reversed: " + myList);
return;
}
//reverse list
//add null as marker
//put first element after null
//remove first element
//do this again until the first element gets null
myList.add(null);
while(myList.get(0) != null){
myList.add(myList.indexOf(null) + 1, myList.get(0));
myList.remove(0);
}
//remove null from list
myList.remove(0);
System.out.println("reversed: " + myList);
}
}