https://www.acmicpc.net/problem/10431
답만 구하기 코드
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int P = Integer.valueOf(br.readLine());
int[][] input= new int[P][21];
for(int i=0; i<P;i++){
String[] temp = br.readLine().split(" ");
for(int j=0; j<21;j++){
input[i][j] = Integer.valueOf(temp[j]);
}
}
for(int i=0;i<P;i++){
int step=0;
for(int j=1; j<21;j++){
int current = input[i][j];
for(int k =1;k<j+1;k++){
if (current<input[i][k]){
step++;
}
}
}
System.out.println(i+1+" "+step);
}
}
}
정렬된 배열까지 구하는 코드
import java.util.Scanner;
class Main
{
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int P = sc.nextInt();
while (P-- > 0) {
int T = sc.nextInt();
int[] h = new int[20];
for (int i = 0; i < 20; i++)
h[i] = sc.nextInt();
int cnt = 0;
for (int i = 0; i < 20; i++) {
for (int j = 0; j < i; j++) {
if (h[j] > h[i]) {
int myH = h[i];
for (int k = i; k > j; k--) {
h[k] = h[k - 1];
cnt++;
}
h[j] = myH;
break;
}
}
}
System.out.println(T + " " + cnt);
}
}
}
'Problem Solving > 구현' 카테고리의 다른 글
BOJ3273 두 수의 합* (1) | 2024.01.29 |
---|---|
BOJ10989 수 정렬하기3 (1) | 2024.01.29 |
BOJ1236 성지키기 (1) | 2024.01.29 |
BOJ10158 개미 (0) | 2024.01.29 |
BOJ13223 소금폭탄 (0) | 2024.01.24 |