First, solve the problem. Then, write the code. ” - John Johnson

It's not at all important to get it right the first time. It's vitally important to get it right the last time. ” - Andrew Hunt and David Thomas

Wednesday, July 16, 2014

Train Swapping

Author : Jisan
Programming language : Java
Difficulty : Easy
Status : Accepted
Problem number : UVA-299
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=235


Code:

import java.util.*;
public class Uva_299 {
    public static void main(String args[]){
    Scanner in=new Scanner(System.in);
    int n,l,c,tmp;
    int[] tomato=new int[10];
    while(in.hasNext()){
    l=in.nextInt();
    c=0;
    for(int i=0;i<l;i++){
    tomato[i]=in.nextInt();
    }
    for(int i=0;i<l;i++){
    for(int j=i+1;j<l;j++){
    if(tomato[i]>tomato[j]){
    c++;
    tmp=tomato[i];
    tomato[j]=tomato[i];
    tomato[j]=tmp;
    }
    }
    }
    System.out.printf("Optimal train swapping takes %d swaps.\n", c);
    }
    }
}

No comments:

Post a Comment