Created
August 19, 2013 09:24
-
-
Save w00lf/6267269 to your computer and use it in GitHub Desktop.
Преобразование последовательности
Задана последовательность, содержащая n целых чисел. Необходимо найти число, которое встречается в этой последовательности наибольшее количество раз, а если таких чисел несколько, то найти минимальное из них, и после этого переместить все такие числа в конец заданной последовательности. Порядок расположения оста…
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")); | |
int count = sc.nextInt(); | |
int[] arr = new int[count]; | |
for (int i = 0; i < count; i++) { | |
arr[i] = sc.nextInt(); | |
} | |
Map<Integer, Integer> m = new HashMap<Integer, Integer>(); | |
int max_ocurr = 0; | |
ArrayList<Integer> max_indexs = new ArrayList(); | |
for (int i = 0; i < arr.length; i++) { | |
int freq = m.get(arr[i]) == null ? 1 : (m.get(arr[i]) + 1); | |
if (freq == max_ocurr) { | |
max_indexs.add(arr[i]); | |
} | |
if (freq > max_ocurr) { | |
max_ocurr = freq; | |
max_indexs.clear(); | |
max_indexs.add(arr[i]); | |
} | |
m.put(arr[i], freq); | |
} | |
int min_ocuur_number = max_indexs.get(0); | |
for (int i = 1; i < max_indexs.size(); i++) { | |
if(min_ocuur_number > max_indexs.get(i)){ | |
min_ocuur_number = max_indexs.get(i); | |
} | |
} | |
pw = new PrintWriter(new File("output.txt")); | |
for (int i = 0; i < arr.length; i++) { | |
if (arr[i] == min_ocuur_number) { | |
continue; | |
} | |
pw.print(arr[i] + " "); | |
} | |
for (int i = 0; i < max_ocurr; i++) { | |
if (i + 1 != max_ocurr ) { | |
pw.print(min_ocuur_number + " "); | |
} else { | |
pw.print(min_ocuur_number); | |
} | |
} | |
pw.close(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment