UVa Problem (Train Swapping) 299:
Train Swapping is a basic problem on UVa online judge for novice problem solver.
You can find details on this Link.
import java.util.Scanner;
/**
* @Author : Muhammad Harun-Or-Roshid
* @Date : Oct 6, 2016
* @Time : 9:24:33 PM
*/
public class UVa299 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testCase = sc.nextInt();
for (int i = 0; i < testCase; i++) {
int inputLines = sc.nextInt();
if (inputLines >= 0 && inputLines <= 50) {
int inputArrays[] = new int[inputLines];
for (int j = 0; j < inputLines; j++) {
inputArrays[j] = sc.nextInt();
}
int swipTimes = 0;
for (int l = 0; l < inputArrays.length; l++) {
for (int k = 0; k < inputArrays.length - 1; k++) {
int a = inputArrays[k];
int b = inputArrays[k + 1];
if (a > b) {
inputArrays[k] = b;
inputArrays[k + 1] = a;
swipTimes++;
}
}
}
System.out.printf("Optimal train swapping takes %d swaps.\n", swipTimes);
}
}
}
}
0 Comments