Skip to content

Instantly share code, notes, and snippets.

@w00lf
Created August 21, 2013 09:00
Show Gist options
  • Save w00lf/6292029 to your computer and use it in GitHub Desktop.
Save w00lf/6292029 to your computer and use it in GitHub Desktop.
Сортировка масс В первой строке входного файла INPUT.TXT находится целое число N (1<=N<=1000) — количество нефтепроводов. В каждой из следующих N строк находится количество (точнее — масса) нефти, транспортированной по соответствующему нефтепроводу за сутки, по одному в строке. Масса нефти задана целым числом от 1 до 10000 с указанием соответств…
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"));
int count = Integer.parseInt(sc.nextLine());
String[] arr = new String[count];
for (int i = 0; i < count; i++) {
arr[i] = sc.nextLine();
}
Map<String, Double> weigths = new HashMap<String, Double>();
String[] weigths_list = {"g", "p", "t"};
weigths.put("g", 0.000001);
weigths.put("p", 0.01638);
weigths.put("t", 1.0);
Map<String, Double> weigths_suffices = new HashMap<String, Double>();
String[] weigths_suffices_list = { "m", "k", "M", "G"};
weigths_suffices.put("m", 0.001);
weigths_suffices.put("k", 1000.0);
weigths_suffices.put("M", 1000000.0);
weigths_suffices.put("G", 1000000000.0);
Map<String, Double> array_weigths = new HashMap<String, Double>();
for (String weigth : weigths_list) {
for (String suffcs : weigths_suffices_list) {
weigths.put(suffcs + weigth, weigths.get(weigth) * weigths_suffices.get(suffcs));
}
}
String[] weigth = new String[2];
Double first_w = 0.0;
Double sec_w = 0.0;
String temp = "";
for (int i = 1; i < arr.length; i++) {
for (int j = i; j > 0; j--) {
if (array_weigths.get(arr[j-1]) == null) {
weigth = arr[j-1].split(" ");
first_w = Double.parseDouble(weigth[0]) * weigths.get(weigth[1]);
array_weigths.put(arr[j-1], first_w);
}else{
first_w = array_weigths.get(arr[j-1]);
}
if (array_weigths.get(arr[j]) == null) {
weigth = arr[j].split(" ");
sec_w = Double.parseDouble(weigth[0]) * weigths.get(weigth[1]);
array_weigths.put(arr[j], sec_w);
}else{
sec_w = array_weigths.get(arr[j]);
}
if (first_w > sec_w) {
temp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = temp;
}
}
}
pw = new PrintWriter(new File("output.txt"));
for (int i = 0; i < arr.length; i++) {
pw.println(arr[i]);
}
pw.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment