12 | ์ ๋ ฌ | ๋ฐฐ์ด์ ์์๋ฅผ ์์๋๋ก ๋์ดํ๋ ์๊ณ ๋ฆฌ์ฆ |
1 | 2750 | ์ ์ ๋ ฌํ๊ธฐ |
์๊ฐ ๋ณต์ก๋๊ฐ O(n²)์ธ ์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค๋ฉด ์ฝ์ ์ ๋ ฌ, ๊ฑฐํ ์ ๋ ฌ ๋ฑ์ด ์์ต๋๋ค. |
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(bfr.readLine());
//ArrayList<Integer> array = new ArrayList<Integer>();
int a[] = new int[T];
for(int i = 0 ; i<T; i++)
a[i] = (Integer.parseInt(bfr.readLine()));
Arrays.sort(a);
for(int i=0; i<T; i++)
bfw.write(a[i] + "\n");
bfw.flush();
bfw.close();
}
}
2 | 2751 | ์ ์ ๋ ฌํ๊ธฐ 2 |
์๊ฐ ๋ณต์ก๋๊ฐ O(nlogn)์ธ ์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ์ผ๋ก ํ ์ ์์ต๋๋ค. ์๋ฅผ ๋ค๋ฉด ๋ณํฉ ์ ๋ ฌ, ํ ์ ๋ ฌ ๋ฑ์ด ์์ง๋ง, ์ด๋ ค์ด ์๊ณ ๋ฆฌ์ฆ์ด๋ฏ๋ก ์ง๊ธ์ ์ธ์ด์ ๋ด์ฅ๋ ์ ๋ ฌ ํจ์๋ฅผ ์ฐ๋ ๊ฒ์ ์ถ์ฒ๋๋ฆฝ๋๋ค. |
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(bfr.readLine());
ArrayList<Integer> array = new ArrayList<Integer>();
for(int i = 0 ; i<T; i++)
array.add(Integer.parseInt(bfr.readLine()));
Collections.sort(array);
for(int i=0; i<T; i++)
bfw.write(array.get(i) + "\n");
bfw.flush();
bfw.close();
}
}
3 | 10989 | ์ ์ ๋ ฌํ๊ธฐ 3 |
์์ ๋ฒ์๊ฐ ์๋ค๋ฉด ์นด์ดํ ์ ๋ ฌ์ ์ฌ์ฉํ์ฌ ๋์ฑ ๋น ๋ฅด๊ฒ ์ ๋ ฌํ ์ ์์ต๋๋ค. |
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader bfr = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bfw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(bfr.readLine());
//ArrayList<Integer> array = new ArrayList<Integer>();
int a[] = new int[T];
for(int i = 0 ; i<T; i++)
a[i] = (Integer.parseInt(bfr.readLine()));
Arrays.sort(a);
for(int i=0; i<T; i++)
bfw.write(a[i] + "\n");
bfw.flush();
bfw.close();
}
}
4 | 2108 | ํต๊ณํ |
์ ๋ ฌ์ ํ์ฉํ๋ ๋ฌธ์ |
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int T = scan.nextInt();
int array[] = new int[T];
for(int i=0; i<T; i++)
array[i] = scan.nextInt();
Arrays.sort(array);
//์ฐ์ ํ๊ท
double sum = 0;
for(int i = 0; i<T; i++)
sum += array[i];
System.out.println(Math.round((double)sum/T));//๋ฐ์ฌ๋ฆผ
//์ค์๊ฐ
System.out.println(array[T/2]);
//์ต๋น๊ฐ
int count[] = new int[T];
int x= 0;
count[0]++;
for(int i = 1; i<T; i++) {
if(array[i] == array[i-1])
count[x] ++;
else {
x = i;
count[x]++;
}
}
int max = 0;
int counting = 0;
for(int i=0; i<T; i++) {
if(max< count[i]) {
max = count[i];
counting = i;
}
}
int testcount = 0;
for(int i=0; i<T; i++) {
if(max == count[i]) testcount++;
if(testcount ==2) {
counting = i;
break;
}
}
System.out.println(array[counting]);
//๋ฒ์
System.out.println(array[array.length-1] - array[0]);
}
}
5 | 1427 | ์ํธ์ธ์ฌ์ด๋ |
์ซ์๋ฅผ ์ ๋ ฌํ๋ ๋ฌธ์ |
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String N = scan.nextLine();
char array[] = new char[N.length()];
for(int i =0; i<N.length(); i++) {
array[i] = N.charAt(i);
}
Arrays.sort(array);
for(int i=array.length-1; i>-1; i--)
System.out.print(array[i]);
}
}
6 | 11650 | ์ขํ ์ ๋ ฌํ๊ธฐ |
์ขํ๋ฅผ ์ ๋ ฌํ๋ ๋ฌธ์ |
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int T = scan.nextInt();
int position[][] = new int [T][2];
for(int i=0; i<T; i++) {
position[i][0] = scan.nextInt();
position[i][1] = scan.nextInt();
}
Arrays.sort(position, new Comparator<int[]>() {
public int compare(int a[], int b[]) {
if(a[0]==b[0]) return a[1]-b[1];
else return a[0] - b[0];
}
});
for(int i=0; i<T; i++) {
System.out.println(position[i][0] + " " +position[i][1]);
}
}
}
7 | 11651 | ์ขํ ์ ๋ ฌํ๊ธฐ 2 |
์ขํ๋ฅผ ๋ค๋ฅธ ์์๋ก ์ ๋ ฌํ๋ ๋ฌธ์ |
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int T = scan.nextInt();
int position[][] = new int [T][2];
for(int i=0; i<T; i++) {
position[i][0] = scan.nextInt();
position[i][1] = scan.nextInt();
}
Arrays.sort(position, new Comparator<int[]>() {
public int compare(int a[], int b[]) {
if(a[1]==b[1]) return a[0]-b[0];
else return a[1] - b[1];
}
});
for(int i=0; i<T; i++) {
System.out.println(position[i][0] + " " +position[i][1]);
}
}
}
8 | 1181 | ๋จ์ด ์ ๋ ฌ |
๋จ์ด์ ์์๋ฅผ ์ ์ํ์ฌ ์ ๋ ฌํ๋ ๋ฌธ์ |
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int T = scan.nextInt();
scan.nextLine();
String array[] = new String[T];
for(int i=0; i<T; i++)
array[i] = scan.nextLine();
Arrays.sort(array, new Comparator<String>() {
public int compare(String a, String b) {
if(a.length() == b.length()) return a.compareTo(b);
else return a.length() - b.length();
}
});
System.out.println(array[0]);
for(int i=1; i<T; i++) {
if(!array[i].equals(array[i-1]))
System.out.println(array[i]);
}
}
}
9 | 10814 | ๋์ด์ ์ ๋ ฌ |
๊ฐ์ด ๊ฐ์ ์์์ ์ ํ๊ด๊ณ๊ฐ ๋ฐ๋์ง ์๋ ์ ๋ ฌ ์๊ณ ๋ฆฌ์ฆ์ ์์ ์ ๋ ฌ(stable sort)์ด๋ผ๊ณ ํฉ๋๋ค. |
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int T = scan.nextInt();
String array[][] = new String [T][2];
for(int i = 0 ; i<T; i++) {
array[i][0] = scan.next();
array[i][1] = scan.next();
}
Arrays.sort(array, new Comparator<String[]>() {
public int compare(String[] a, String[] b) {
return Integer.parseInt(a[0]) - Integer.parseInt(b[0]);
}
});
for(int i=0; i<T; i++) {
System.out.println(array[i][0] + " " + array[i][1]);
}
}
}
728x90
๋ฐ์ํ