0228 Review

좋은 알고리즘을 얻고 이해하기 위해

 많은 코드들을 뜯고 이해해라, 이해하기 힘들다면 최소한 그 코드를 사용할 수 있도록 기록해라. 이미 발명되어 있는 바퀴의 존재를 알지 못해서 새로운 바퀴를 만들어내는 시간낭비를 하지 않도록 더 많이 찾아보고 알아보는 눈이 필요하다.

오늘의 코드

랜덤 배열 오름차순, 내림차순 정렬 알고리즘

package study0228;

import java.util.Arrays;

public class ArrTest_Sort {
	
	public static void main(String[] args) {
		
		//최대값 최소값 정렬 (오름차순)
		int[] score = new int[10];
		for(int i=0; i<score.length; i++) {
			score[i] = (int)(Math.random()*100+1);
		}
		System.out.println("1-1. 원본배열");
		System.out.println(Arrays.toString(score));
		
		for(int i=0; i<score.length-1; i++) {
			boolean skip = true;
			for(int j=0; j<score.length-1; j++) {
				if(score[j]>score[j+1]) {
					int tmp = score[j];
					score[j] = score[j+1];
					score[j+1] = tmp;
					skip = false;
				}
			}
			if(skip) {
				break;
			}
		}
		System.out.println("1-2. 오름차순");
		System.out.println(Arrays.toString(score));
		
		System.out.println();
		
		//최대값 최소값 정렬 (내림차순)
		int[] score2 = new int[10];
		for(int i=0; i<score2.length; i++) {
			score2[i] = (int)(Math.random()*100+1);
		}
		System.out.println("2-1. 원본배열");
		System.out.println(Arrays.toString(score2));
		
		for(int i=score2.length-1; i>0; i--) {
			boolean skip = true;
			for(int j=score2.length-1; j>0; j--) {
				if(score2[j]>score2[j-1]) {
					int tmp = score2[j];
					score2[j] = score2[j-1];
					score2[j-1] = tmp;
					skip = false;
				}
			}
			if(skip) {
				break;
			}
		}
		System.out.println("2-2. 내림차순");
		System.out.println(Arrays.toString(score2));
 		
	}
}

야구게임 코드 간소화

//메인 클래스
package study0228;

import java.util.Arrays;

public class ArrTest_Sort {
	
	public static void main(String[] args) {
		
		//최대값 최소값 정렬 (오름차순)
		int[] score = new int[10];
		for(int i=0; i<score.length; i++) {
			score[i] = (int)(Math.random()*100+1);
		}
		System.out.println("1-1. 원본배열");
		System.out.println(Arrays.toString(score));
		
		for(int i=0; i<score.length-1; i++) {
			boolean skip = true;
			for(int j=0; j<score.length-1; j++) {
				if(score[j]>score[j+1]) {
					int tmp = score[j];
					score[j] = score[j+1];
					score[j+1] = tmp;
					skip = false;
				}
			}
			if(skip) {
				break;
			}
		}
		System.out.println("1-2. 오름차순");
		System.out.println(Arrays.toString(score));
		
		System.out.println();
		
		//최대값 최소값 정렬 (내림차순)
		int[] score2 = new int[10];
		for(int i=0; i<score2.length; i++) {
			score2[i] = (int)(Math.random()*100+1);
		}
		System.out.println("2-1. 원본배열");
		System.out.println(Arrays.toString(score2));
		
		for(int i=score2.length-1; i>0; i--) {
			boolean skip = true;
			for(int j=score2.length-1; j>0; j--) {
				if(score2[j]>score2[j-1]) {
					int tmp = score2[j];
					score2[j] = score2[j-1];
					score2[j-1] = tmp;
					skip = false;
				}
			}
			if(skip) {
				break;
			}
		}
		System.out.println("2-2. 내림차순");
		System.out.println(Arrays.toString(score2));
 		
	}
}
//라이브러리
package study0225;

import java.util.Scanner;

public class ArrBaseballLib {

	Scanner scan = new Scanner(System.in);
	//컴퓨터 랜덤값 중복방지 메서드
	public void makeEachInt(int[] a) {
		while(true) {
			for(int i=0; i<a.length; i++) {
				a[i] = (int)(Math.random()*9+1);
			}
			if(a[0]!=a[1] && a[0]!=a[2] && a[1]!=a[2]) {
				break;
			}
		}
	}
    
	//입력값 배열변경 메서드
	public int[] toArray() {
		int[] copyArr = new int[3];

		System.out.println("세자리 숫자를 입력하세요.");
		while(true) {
			String s = scan.next();
			if(s.length()<3) {
				System.out.println("Error : 세자릿수를 입력하세요.");
			} else {
				copyArr[0] = s.charAt(0)-'0';
				copyArr[1] = s.charAt(1)-'0';
				copyArr[2] = s.charAt(2)-'0';

				if(copyArr[0]==copyArr[1] || copyArr[0]==copyArr[2] || copyArr[1]==copyArr[2]) {
					System.out.println("Erorr : 겹치지 않는 숫자를 입력하세요.");								
				} else if (copyArr[0]==0 || copyArr[1]==0 || copyArr[1]==0){
					System.out.println("Error : 0을 제외하고 입력하세요.");
				} else {
					return copyArr;
				}
			}
		}
	}
	
    //두 배열 비교 메서드
	public int[] compareArr(int[] a, int[] b) {

		int strike = 0;
		int ball = 0;
		int out = 0;

		for(int i=0; i<a.length; i++) {
			for(int j=0; j<b.length; j++) {
				if(a[i] == b[j]) {
					if(i == j) {
						strike++;
					} else {
						ball++;
					}
				}
			}
			if(i == a.length-1) {
				out = a.length-(strike+ball);
			}
		}
		
		int[] arr = new int[3];
		arr[0] = strike;
		arr[1] = ball;
		arr[2] = out;

		return arr;
	}

}

좋은 웹페이지 즐겨찾기