Skip to content

Instantly share code, notes, and snippets.

@st0le
Created January 15, 2015 01:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save st0le/44bed35738544776f130 to your computer and use it in GitHub Desktop.
Save st0le/44bed35738544776f130 to your computer and use it in GitHub Desktop.
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