Created
January 15, 2015 01:01
-
-
Save st0le/44bed35738544776f130 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.PrintStream; | |
import java.util.Scanner; | |
public class Solution { | |
public static void main(String[] args) throws Exception { | |
new Solution().go(); | |
} | |
private void swap(char[] arr, int i, int j) { | |
char c = arr[i]; | |
arr[i] = arr[j]; | |
arr[j] = c; | |
} | |
private void go() throws Exception { | |
System.setOut(new PrintStream(new File("C:\\Users\\gaurav\\Desktop\\out.txt"))); | |
Scanner scan = new Scanner(new File( | |
"C:\\Users\\gaurav\\Desktop\\in.txt")); | |
for (int T = Integer.parseInt(scan.nextLine()), t = 1; t <= T; t++) { | |
String num = scan.nextLine(); | |
long min = Long.parseLong(num), max = min; | |
char[] foo = num.toCharArray(); | |
for (int i = 0; i < foo.length; i++) { | |
for (int j = i + 1; j < foo.length; j++) { | |
swap(foo, i, j); | |
if (foo[0] != '0') { | |
long n = Long.parseLong(new String(foo)); | |
if (n < min) | |
min = n; | |
if (n > max) | |
max = n; | |
} | |
swap(foo, i, j); | |
} | |
} | |
System.out.println(String.format("Case #%d: %d %d", t, min, max)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment