728x90
[Java] 쌓아 올리기 Stack'em up
문제 설명
입력
2
2
2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 52 51
52 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 1
1
2
2
2 1 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 52 51
52 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 1
1
2
출력결과
King of Spades
2 of Clubs
4 of Clubs
5 of Clubs
6 of Clubs
7 of Clubs
8 of Clubs
9 of Clubs
10 of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Clubs
2 of Diamonds
3 of Diamonds
4 of Diamonds
5 of Diamonds
6 of Diamonds
7 of Diamonds
8 of Diamonds
9 of Diamonds
10 of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Ace of Diamonds
2 of Hearts
3 of Hearts
4 of Hearts
5 of Hearts
6 of Hearts
7 of Hearts
8 of Hearts
9 of Hearts
10 of Hearts
Jack of Hearts
Queen of Hearts
King of Hearts
Ace of Hearts
2 of Spades
3 of Spades
4 of Spades
5 of Spades
6 of Spades
7 of Spades
8 of Spades
9 of Spades
10 of Spades
Jack of Spades
Queen of Spades
Ace of Spades
3 of Clubs
King of Spades
2 of Clubs
4 of Clubs
5 of Clubs
6 of Clubs
7 of Clubs
8 of Clubs
9 of Clubs
10 of Clubs
Jack of Clubs
Queen of Clubs
King of Clubs
Ace of Clubs
2 of Diamonds
3 of Diamonds
4 of Diamonds
5 of Diamonds
6 of Diamonds
7 of Diamonds
8 of Diamonds
9 of Diamonds
10 of Diamonds
Jack of Diamonds
Queen of Diamonds
King of Diamonds
Ace of Diamonds
2 of Hearts
3 of Hearts
4 of Hearts
5 of Hearts
6 of Hearts
7 of Hearts
8 of Hearts
9 of Hearts
10 of Hearts
Jack of Hearts
Queen of Hearts
King of Hearts
Ace of Hearts
2 of Spades
3 of Spades
4 of Spades
5 of Spades
6 of Spades
7 of Spades
8 of Spades
9 of Spades
10 of Spades
Jack of Spades
Queen of Spades
Ace of Spades
3 of Clubs
정답 코드
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
int testcase_num = scanner.nextInt();
scanner.nextLine();
scanner.nextLine();
for(int i=0; i<testcase_num; i++){
int n=scanner.nextInt();
scanner.nextLine();
int mix[][]=new int[n][52];
for(int j=0; j<n;j++){
for(int k=0; k<52;k++){
mix[j][k]= scanner.nextInt()-1;
}
scanner.nextLine();
}
int mix_od[] = new int[52];
for(int y=0; y<52;y++)
mix_od[y]=y;
String[] pair = new String[52];
String[] card_num = {"2","3","4","5","6", "7", "8", "9","10", "Jack", "Queen", "King", "Ace"};
String[] card_class = {"Clubs","Diamonds", "Hearts", "Spades"};
int index=0;
for(int j=0; j<4; j++){
for(int k=0;k<13;k++){
pair[index++] = card_num[k]+" of "+card_class[j];
}
}
/*********************여기까지가 사용자 입력 받아 기본 셋팅**************************/
/******************카드 섞기 입력받아 섞는 알고리즘*****************************/
while(scanner.hasNextLine()){ //다음 입력이 있을때 까지 계속 받는다
String seq= scanner.nextLine();
if(seq.equals("")){ //공백이라면 반복문 탈출
break;
}
int sequence = Integer.parseInt(seq)-1; //입력받은숫자 index숫자로 변환
int[] newMix = new int[52]; //섞은 결과를 잠시 저장할 배열 선언
for(int j=0; j<52;j++)
newMix[j]= mix_od[mix[sequence][j]]; //섞는 방법을 이용하여 섞어진 순서 index를 newMix에 저장
mix_od =newMix.clone(); 깊은복사로 mix_od 업뎃
}
/*********************************섞어진 카드 출력하기***********************/
for(int j=0; j<52; j++){
System.out.println(pair[mix_od[j]]);
}
System.out.println("");
}
}
}
🔑 Key Point 🔑
새로운 배열에 섞는 방법을 적용하여 기존의 정렬된 것을 하나하나 넣는다.
그리고 깊은복사로 업데이트 한다.
728x90
반응형
'💡 CodingTest > UVa' 카테고리의 다른 글
[Java] WERTYU 문제 - UVa 10082번 Programming Challenges 문제 17번 (0) | 2020.09.26 |
---|---|
[Java] 공통된 변경 문자열(Common Permutation) (0) | 2020.09.22 |
[Java] 동맹 휴업 Hartals (0) | 2020.09.11 |
[Java] 포커 패 Poker Hands (0) | 2020.09.11 |
[Java] 유쾌한 점퍼 Jolly Jumper (0) | 2020.09.09 |