UVA Solution 299 - Train Swapping - Solution in C++ | Volume 2 - Online Judge Solution

Latest

It is a free Online judges problems solution list. Here you can find UVA online Judge Solution, URI Online Judge Solution, Code Marshal Online Judge Solution, Spoz Online Judge Problems Solution

Monday, May 1, 2017

UVA Solution 299 - Train Swapping - Solution in C++ | Volume 2

UVA Solution 299 - Train Swapping - Solution in C++ | Volume 2


UVA Online Judge Solution 299 - Train Swapping | Volume 2
UVA Problem Link - 299 - Train Swapping https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=4&page=show_problem&problem=235

Problem Name: 299 - Train Swapping
Problem Number : UVA - 299 - Train Swapping
Online Judge : UVA Online Judge Solution
Volume: 2
Solution Language : C plus plus

UVA Solution 299 - Train Swapping  Code in CPP:


#include<stdio.h>
int main() {
 int N, L, A[50];
 scanf("%d", &N);
 while(N--) {
  scanf("%d", &L);
  int i, j, count = 0, tmp;
  for(i = 0; i < L; i++)
   scanf("%d", &A[i]);
  for(i = 0; i < L; i++) {
   for(j = L-1; j > i; j--) {
    if(A[j] < A[j-1]) {
     tmp = A[j];
     A[j] = A[j-1];
     A[j-1] = tmp;
     count++;
    }
   }
  }
  printf("Optimal train swapping takes %d swaps.\n", count);
 }
    return 0;
}

No comments:

Post a Comment