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

Sunday, July 20, 2014

Children’s Game

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

Help: http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html

Code:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Arrays;

public class Uva_10905 {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        while (true) {
            int n = Integer.parseInt(br.readLine());
            if (n == 0) {
                break;
            }
            Name[] arr = new Name[n];
            String[] str = br.readLine().split(" ");
            for (int i = 0; i < n; i++) {
                arr[i] = new Name(str[i]);
            }
            Arrays.sort(arr);
            for (int i = arr.length - 1; i > -1; i--) {
                sb.append(arr[i].getX());
            }
            sb.append("\n");
        }
        System.out.print(sb);
    }
}

class Name implements Comparable<Name> {

    String x;

    public String getX() {
        return x;
    }

    public Name(String x) {
        this.x = x;
    }

    public int compareTo(Name o) {
        BigInteger bg1=new BigInteger(x+o.getX());
        BigInteger bg2=new BigInteger(o.getX()+x);
        return bg1.compareTo(bg2);
    }
}

No comments:

Post a Comment