Created
August 21, 2013 12:20
-
-
Save w00lf/6293773 to your computer and use it in GitHub Desktop.
Годовой баланс
Входные данные Входной файл INPUT.TXT содержит два целых числа a и b (-109 < a,b < 109). Выходные данные В выходной файл OUTPUT.TXT выведите одно целое число – наибольшую разность чисел, первое из которых может быть получено перестановкой цифр a, а второе – перестановкой цифр b.
http://acmp.ru/index.asp?main=task&id_task=32
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.util.*; | |
import java.io.*; | |
public class Main{ | |
public static void main(String[] argv) throws IOException{ | |
new Main().run(); | |
} | |
PrintWriter pw; | |
Scanner sc; | |
public void run() throws IOException{ | |
sc = new Scanner(new File("input.txt")); | |
String input_text = sc.nextLine(); | |
Boolean input_minus = false; | |
if (input_text.charAt(0) == '-') { | |
input_minus = true; | |
input_text = input_text.substring(1); | |
} | |
String[] input = input_text.split("(?!^)"); | |
String output_text = sc.nextLine(); | |
Boolean output_minus = false; | |
if (output_text.charAt(0) == '-') { | |
output_minus = true; | |
output_text = output_text.substring(1); | |
} | |
String[] output = output_text.split("(?!^)"); | |
String temp = ""; | |
if (input_minus) { | |
insert_sort(input, input_minus, true); | |
} else { | |
insert_sort(input, input_minus, false); | |
} | |
if (output_minus) { | |
insert_sort(output, !output_minus, false); | |
}else{ | |
insert_sort(output, !output_minus, true); | |
} | |
int fake_input = to_integer(input, input_minus); | |
int fake_output = to_integer(output, output_minus); | |
pw = new PrintWriter(new File("output.txt")); | |
pw.print(fake_input - fake_output); | |
pw.close(); | |
} | |
public void insert_sort(String[] input, Boolean reverse, Boolean no_first_ziro){ | |
Boolean comparition = false; | |
String temp = ""; | |
int first = 0; | |
int second = 0; | |
if(input.length > 1) { | |
for (int i = 1; i < input.length; i++) { | |
for (int j = i; j > 0; j--) { | |
first = Integer.parseInt(input[j-1]); | |
second = Integer.parseInt(input[j]); | |
if (reverse) { | |
comparition = first > second; | |
}else { | |
comparition = first < second; | |
} | |
if(comparition){ | |
temp = input[j-1]; | |
input[j-1] = input[j]; | |
input[j] = temp; | |
} | |
} | |
} | |
if (no_first_ziro) { | |
if (Integer.parseInt(input[0]) == 0) { | |
for (int i = 0; i < input.length; i++) { | |
if (Integer.parseInt(input[i]) != 0) { | |
input[0] = input[i]; | |
input[i] = "0"; | |
break; | |
} | |
} | |
} | |
} | |
} | |
} | |
public int to_integer(String[] input, Boolean input_minus){ | |
String temp = ""; | |
for (String string : input) { | |
temp += string; | |
} | |
int sign = 1; | |
if (input_minus) { | |
sign = -1; | |
} | |
return sign * Integer.parseInt(temp); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment