package week4;
import java.util.Scanner;
import java.util.Random;
public class 정렬하기 {
static Scanner input = new Scanner(System.in);
public static int [] makeArray(int n) {
int [] ar = new int[n];
for(int i=0; i<n; i++) {
ar[i]=0;
}
return ar;
}
public static void setArrayRandom(int [] ar, int start, int end) {
Random r = new Random();
for(int i=0; i<ar.length; i++) {
// r.nextInt() -> -21억~21억 이내 정수 리턴
// r.nextInt(n) -> 0~n미만의 정수 리턴
ar[i]=r.nextInt(end-start+1)+start;
// ar[i]=(int)(Math.random()*(end-start+1))+start;
// Math.random -> 0.0부터 1.0미만의 실수 리턴
}
}
public static void sortArray1(int [] ar) { // bubbleSort
for(int scan=0; scan<ar.length-1; scan++) { // 0~n-1까지 반복
for(int i=0; i<ar.length-1-scan; i++) {
if(ar[i]>ar[i+1]) { // i가 i+1보다 크면 swap
int temp=ar[i+1];
ar[i+1]=ar[i];
ar[i]=temp;
}
}
}
}
public static void sortArray2(int [] ar) { // bubbleSort
for(int scan=0; scan<ar.length-1; scan++) { // 0~n-1까지 반복
for(int i=0; i<ar.length-1-scan; i++) {
if(ar[i]<ar[i+1]) { // i가 i+1보다 작으면 swap
int temp=ar[i+1];
ar[i+1]=ar[i];
ar[i]=temp;
}
}
}
}
public static void showArray(int [] ar) {
for(int i=0; i<ar.length; i++) {
System.out.print(ar[i] + " ");
}
System.out.println();
}
public static void main(String[] args) {
System.out.print("배열 크기: ");
int n = input.nextInt();
// 배열 생성, 초기화
int [] arr = makeArray(n);
// 랜덤수로 초기화
System.out.print("시작 수: ");
int s = input.nextInt();
System.out.print("끝 수: ");
int e = input.nextInt();
setArrayRandom(arr, s, e);
System.out.println();
System.out.print("sort 전: ");
showArray(arr);
sortArray1(arr);
System.out.print("sort(오름차순): ");
showArray(arr);
sortArray2(arr);
System.out.print("sort(내림차순): ");
showArray(arr);
}
}
소감: 메소드 개수가 많아서 시간이 생각보다 오래 소요되었다. 각각 인자와 리턴값을 생각해야 하는 점이 헷갈렸다. sortArray1을 복사해서 sortArray2를 만드는 등 수정이 용이했다.
package week4;
import java.util.Scanner;
public class 극장예약시스템 {
static Scanner input = new Scanner(System.in);
public static void 예약하기(int [][] ar) {
// 행, 열 입력
System.out.print("예약할 행을 입력하세요: ");
int row = input.nextInt();
System.out.print("예약할 열을 입력하세요: ");
int col = input.nextInt();
// 범위 체크
if(row<1 || row>10 || col<1 || col>10) {
System.out.println("좌석 범위 밖입니다.");
return;
}
// 빈자리 체크
if(ar[row-1][col-1]==1) { // 1~10 -> 0~9
System.out.println("이미 예약된 자리입니다.");
return;
}
else {
ar[row-1][col-1]=1;
System.out.println("예약되었습니다.");
}
}
public static void 취소하기(int [][] ar) {
// 행, 열 입력
System.out.print("취소할 행을 입력하세요: ");
int row = input.nextInt();
System.out.print("취소할 열을 입력하세요: ");
int col = input.nextInt();
// 범위 체크
if(row<1 || row>10 || col<1 || col>10) {
System.out.println("좌석 범위 밖입니다.");
return;
}
// 예약 여부 체크
if(ar[row-1][col-1]==0) { // 1~10 -> 0~9
System.out.println("예약되지 않은 자리입니다.");
return;
}
else {
ar[row-1][col-1]=0;
System.out.println("취소되었습니다.");
}
}
public static void 좌석보이기(int [][] ar) {
for(int i=0; i<ar.length; i++) {
System.out.printf("[%2d]: ", i+1);
for(int j=0; j<ar.length; j++) {
if(ar[i][j]==0) System.out.print("□ ");
else System.out.print("■ ");
}
System.out.println();
}
}
public static void 메뉴보이기() {
System.out.println();
System.out.println("=== 메뉴 ===");
System.out.println("1.좌석보이기 2.예약하기 3.취소하기 ... 0.종료");
System.out.println("===========");
}
public static void welcome() {
System.out.println("극장 예약 시스템을 시작합니다.");
}
public static void main(String[] args) {
int [][] seat = new int[10][10];
welcome();
while(true) {
메뉴보이기();
System.out.print("메뉴: ");
int menu = input.nextInt();
if(menu==0) break;
else if(menu==1) 좌석보이기(seat);
else if(menu==2) 예약하기(seat);
else if(menu==3) 취소하기(seat);
else System.out.println("올바른 메뉴를 입력해주세요.");
}
System.out.println("극장 예약 시스템을 종료합니다.");
}
}
소감: 11x11짜리 배열을 만들어 인덱스 1~10을 활용해도 편리할 것 같다.
package week4;
import java.util.Scanner;
import java.util.Random;
public class MyBuilding {
static Scanner input = new Scanner(System.in);
public static void 메뉴보이기() {
System.out.println();
System.out.println("=== !! Welcome to 마이빌딩 !! ===");
System.out.println("1.빌딩만들기 2.입주하기 3.퇴실하기 4.층리모델링 5.방리모델링 6.빌딩현황보기 7.랜덤입주이벤트 0.종료");
System.out.println("==============================");
}
public static int [][] 빌딩만들기(int [][] ar) { // 새로 할당하는 경우 배열을 리턴해야 함
System.out.print("층 개수를 입력하세요: ");
int row=input.nextInt();
ar = new int[row][];
for(int i=0; i<row; i++) {
System.out.printf("%d층의 방 개수를 입력하세요: ", i+1);
int col=input.nextInt();
ar[i]=new int[col];
for(int j=0; j<col; j++) ar[i][j]=0; // 초기화
}
return ar;
}
public static void 입주하기(int [][] ar) {
System.out.print("입주할 층을 입력하세요: ");
int row=input.nextInt();
System.out.print("입주할 방 번호를 입력하세요: ");
int col=input.nextInt();
// 범위 체크
if(row<1 || row>ar.length || col<1 || col>ar[row-1].length) {
System.out.println("존재하지 않는 방입니다.");
return;
}
if(ar[row-1][col-1]==0) {
System.out.print("입주할 인원을 입력하세요: ");
int num=input.nextInt();
ar[row-1][col-1]=num;
System.out.printf(">> %d층 %d호실에 %d명이 입주했습니다.", row, col, num);
}
else System.out.println("이미 세입자가 있습니다.");
}
public static void 퇴실하기(int [][] ar) {
System.out.print("퇴실할 층을 입력하세요: ");
int row=input.nextInt();
System.out.print("퇴실할 방 번호를 입력하세요: ");
int col=input.nextInt();
// 범위 체크
if(row<1 || row>ar.length || col<1 || col>ar[row-1].length) {
System.out.println("존재하지 않는 방입니다.");
return;
}
if(ar[row-1][col-1]==0) {
System.out.println("세입자가 없습니다.");
}
else {
int num=ar[row-1][col-1];
ar[row-1][col-1]=0;
System.out.printf(">> %d층 %d호실에 %d명이 퇴실했습니다.", row, col, num);
}
}
public static int [][] 층리모델링(int [][] ar) {
System.out.print("변경할 층 수를 입력하세요: ");
int row=input.nextInt();
if(row>0) { // 층 추가
int [][] newAr = new int[ar.length+row][];
for(int i=0; i<newAr.length; i++) {
if(i<ar.length) {
newAr[i]=ar[i];
}
else {
System.out.printf("%d층의 방 개수를 입력하세요: ", i+1);
int col=input.nextInt();
newAr[i]=new int[col];
for(int j=0; j<col; j++) newAr[i][j]=0;
}
}
return newAr;
}
else if(row<0) { // 층 삭제
int cnt=0; // 입주자 있는지 확인
for(int i=ar.length-1; i>ar.length-row-1; i--) {
for(int j=0; j<ar[i].length; j++) {
cnt+=ar[i][j];
if(cnt!=0) break;
}
if(cnt!=0) break;
}
if(cnt==0) { // 삭제 가능
int [][] newAr = new int[ar.length-row][]; // 남은 층 복제
for(int i=0; i<newAr.length; i++) {
newAr[i]=ar[i];
}
return newAr;
}
else {
System.out.println("입주자가 있으므로 변경이 불가능합니다.");
return ar;
}
}
return ar; // row==0
}
public static void 방리모델링(int [][] ar) {
System.out.print("변경할 층 수를 입력하세요: ");
int row=input.nextInt();
System.out.print("변경할 방 수를 입력하세요: ");
int col=input.nextInt();
if(col>0) { // 방 추가
int [] temp = new int[ar[row-1].length+col];
for(int i=0; i<temp.length; i++) {
if(i<ar[row-1].length) temp[i]=ar[row-1][i]; // 입주자 그대로
else temp[i]=0; // 추가한 방
}
ar[row-1]=temp;
return;
}
else if(col<0) { // 방 삭제
int cnt=0; // 입주자 있는지 확인
for(int i=0; i<ar[row-1].length; i++) {
cnt+=ar[row-1][i];
if(cnt!=0) break;
}
if(cnt==0) { // 삭제 가능
int [] temp = new int[ar[row-1].length+col];
for(int i=0; i<temp.length; i++) {
temp[i]=ar[row-1][i];
}
ar[row-1]=temp;
return;
}
else {
System.out.println("입주자가 있으므로 변경이 불가능합니다.");
return;
}
}
return;
}
public static void 빌딩현황보기(int [][] ar) {
System.out.println("=== 빌딩 현황(입주인원) ===");
for(int i=0; i<ar.length; i++) {
System.out.printf("%d층: ", i+1);
for(int j=0; j<ar[i].length; j++)
System.out.printf("[%d]", ar[i][j]);
System.out.println();
}
System.out.println("======================");
}
public static void 랜덤입주이벤트(int [][] ar) {
Random r = new Random();
int rand = r.nextInt(2);
if(rand==1) {
while(true) {
int row = r.nextInt(ar.length);
if(ar[row].length==0) continue; // 방 없음
int col = r.nextInt(ar[row].length);
if(ar[row][col]!=0) continue; // 입주자가 있는 방
int num = r.nextInt(9)+1; // 1~9
ar[row][col]=num;
System.out.printf("오늘은 %d층 %d호실에 %d명이 새로 입주했습니다.\n", row+1, col+1, num);
return;
}
}
else System.out.println("오늘은 입주한 사람이 없습니다.");
}
public static void main(String[] args) {
int [][] building = null;
while(true) {
메뉴보이기();
int menu=input.nextInt();
if(menu==1) {
if(building==null) building = 빌딩만들기(building);
else System.out.println("이미 빌딩이 존재합니다.");
}
else if(menu==2) 입주하기(building);
else if(menu==3) 퇴실하기(building);
else if(menu==4) building = 층리모델링(building);
else if(menu==5) 방리모델링(building);
else if(menu==6) 빌딩현황보기(building);
else if(menu==7) 랜덤입주이벤트(building);
else if(menu==0) return;
else System.out.println("올바른 메뉴를 입력하세요.");
}
}
}
소감: 랜덤입주이벤트에서 입주하기 메소드를 호출하려고 했었는데, 인자와 메인 메소드를 수정하다보니 더 복잡해져서 랜덤입주이벤트 안에 새로 작성하였다. 입주하기 메소드를 더 작게 나누면 가능하겠지만 깔끔하지 않아 보인다. 어디까지 메소드를 분리할지가 애매한 것 같다.
package week4;
public class RotateImage {
public static int [][] rotateLeft(int [][] A) {
int [][] B = new int[A.length][A[0].length];
for(int i=0; i<A.length; i++) {
for(int j=0; j<A[i].length; j++) {
B[A.length-j-1][i] = A[i][j];
}
}
return B;
}
public static int [][] rotateRight(int [][] A) {
int [][] B = new int[A.length][A[0].length];
for(int i=0; i<A.length; i++) {
for(int j=0; j<A[i].length; j++) {
B[j][A.length-i-1] = A[i][j];
}
}
return B;
}
public static int [][] flipDown(int [][] A) {
int [][] B = new int[A.length][A[0].length];
for(int i=0; i<A.length; i++) {
for(int j=0; j<A[i].length; j++) {
B[A.length-i-1][j] = A[i][j];
}
}
return B;
}
public static void showImage(int [][] img) {
for(int i=0; i<img.length; i++) {
for(int j=0; j<img[i].length; j++) {
if(img[i][j]==1) System.out.print("●");
else System.out.print("○");
}
System.out.println();
}
System.out.println();
System.out.println();
}
public static void main(String[] args) {
//스마일배열
int [][] smile = {
{ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } ,
{ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } ,
{ 0 , 0 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 0 , 0 } ,
{ 0 , 1 , 0 , 1 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 0 , 1 , 0 } ,
{ 0 , 1 , 0 , 1 , 0 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 0 , 1 , 0 } ,
{ 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } ,
{ 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } ,
{ 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } ,
{ 0 , 0 , 1 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 } ,
{ 0 , 0 , 1 , 0 , 0 , 0 , 1 , 1 , 1 , 0 , 0 , 0 , 1 , 0 , 0 } ,
{ 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 } ,
{ 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 } ,
{ 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 } ,
{ 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 , 1 , 0 , 0 , 0 , 0 , 0 } ,
{ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 }
};
//하트배열
int [][] heart = {
{0,0,1,1,0,0,0,0,0,1,1,0,0 } ,
{0, 1,0,0,1,0,0,0,1,0,0,1,0 } ,
{1,0,0,0,0,1,0,1,0,0,0,0,1 } ,
{1,0,0,0,0,0,1,0,0,0,0,0,1 } ,
{1,0,0,0,0,0,0,0,0,0,0,0,1 } ,
{1,0,0,0,0,0,0,0,0,0,0,0,1 } ,
{1,0,0,0,0,0,0,0,0,0,0,0,1 } ,
{0,1,0,0,0,0,0,0,0,0,0,1,0 } ,
{0,0,1,0,0,0,0,0,0,0,1,0,0 } ,
{0,0,0,1,0,0,0,0,0,1,0,0,0 } ,
{0,0,0,0,1,0,0,0,1,0,0,0,0 } ,
{0,0,0,0,0,1,0,1,0,0,0,0,0 } ,
{0,0,0,0,0,0,1,0,0,0,0,0,0 }
};
showImage(smile);
showImage(rotateLeft(smile));
showImage(rotateRight(smile));
showImage(heart);
showImage(flipDown(heart));
}
}



소감: 메소드 마지막줄에서 배열 리턴 대신 A=B;를 하니까 원본 배열에 반영이 안되었다. 메소드에서의 배열을 더 자세히 공부해야 할 것 같다.