java 순서바꾸기(temp), 오름차순(Arrays.sort()), 중복처리(lotto 예시)

limchard·2023년 10월 26일
0

java

목록 보기
34/48

기본형 (순서바꾸기)

순서바꾸기를 해보자.
보통 temp라고 많이 사용한다고 한다.
temp는 빈 공간 하나를 주어서 한칸씩 돌린다고 생각하면 편하다.

// 순서바꾸기
int a=10,b=20;

System.out.println("a"+a+",b="+b);
		
int temp=a;
a=b;
b=temp;		
System.out.println("a"+a+",b="+b);
	

System.out.println("배열로 순서 바꾸기");	
int[] n= {5,7,9};		
for(int t:n) 
	System.out.print(t+"\t");
System.out.println("\n0번과 2번 교환후 출력");	
int temp1=n[0];
n[0]=n[2];
n[2]=temp1;
		
for(int t:n)
	System.out.print(t+"\t");

결과

a10,b=20
a20,b=10
배열로 순서 바꾸기
5 7 9
0번과 2번 교환후 출력
9 7 5

순서순서 바꾸기 (중심 기준으로 양 옆을 데칼코마니처럼 순서 바꾸기)

순서를 바꿔보자.
여기서 주의할점은 for문의 조건식이다.
// 데이터 거꾸로 변경하기. 에서 for문 조건식을 보자.
arr.length/2 로 되어있다. 어차피 반만 바꾸면 나머지 반도 바뀌기 때문에 /2를 해준다.

	int[] arr= {99,6,8,9,22,344,53,476,682,97,75,33,411,42,354};
		
	// 출력
	System.out.println("원래 데이터");
		
	for(int i=0;i<arr.length;i++) {
		System.out.print(arr[i]+"  ");
	}
	System.out.println();
		
	// 데이터 거꾸로 변경하기.
	for(int i=0;i<arr.length/2;i++) {
		int temp=arr[i];
		arr[i]=arr[arr.length-1-i]  ; // 10개 일때 0하고 9번이랑 바꾸는 것.
		arr[arr.length-1-i]=temp;
	}
		
	// 출력
	System.out.println("순서 바꾼 데이터");
		
	for(int i=0;i<arr.length;i++) {
		System.out.print(arr[i]+"  ");
	}

결과

원래 데이터
99 26 48 59 22 34 53 76 82 97 75 33 11 42 54
순서 바꾼 데이터
54 42 11 33 75 97 82 76 53 34 22 59 48 26 99

오름차순 정렬

오름차순 정렬에는 2가지가 있다.

1. for문 노가다

반복을 통해 순서를 바꿔준다.
순서를 바꿔주는 경우도 2개씩 묶어서 생각한다.
기준은 앞글자이다.
그래서 첫번째 for문의 조건은 arr.length-1개이다.
두번째는 0이 아닌 1에서 시작해서 arr.length 개로 진행한다.
이렇게 비교하여 앞 글자가 뒷 글자보다 클 경우에 temp를 이용하여 숫자 위치를 바꿔준다.

	// 기준데이터는 0부터 끝에서 두번째까지
	for(int i=0;i<arr.length-1;i++) {
		// 비교하는 데이터는 기준 다음값부터 끝가지.
		for(int j=i+1;j<arr.length;j++) {
			if(arr[i]>arr[j]) {
				int temp=arr[i];
				arr[i]=arr[j];
				arr[j]=temp;
			}
		}
	}
	// 오름차순 후의 데이터
	System.out.println("\n오름차순 후 데이터");
				
	for(int i=0;i<arr.length;i++) {
		System.out.print(arr[i]+"  ");
	}

결과

오름차순 후 데이터
54 42 11 33 75 97 82 76 53 34 22 59 48 26 99

2. selection sort(Arrays.sort)

위와 같이 Arrays.sort를 이용하여 간단하게 오름차순 정렬을 할 수 있다.
여기서 배열 이름은 arr이다.

	Arrays.sort(arr);
	for(int i=0;i<arr.length;i++) {
	System.out.print(arr[i]+"  ");
    }

결과

11 22 26 33 34 42 48 53 54 59 75 76 82 97 99

중복처리 및 lotto 예시

lotto 예시이다.

나머지 오름차순, 랜덤수 등은 전에 해봤던 사항이니 참고하길 바라며, 이번 예시에서는 중복처리를 잘 보자.

for문 안에서 if에 중복조건을 준다. 중복이 되었을 경우 자릿수를 0의 자릿수와 1의 자릿수 숫자가 같으면 1의 자릿수 숫자를 다시 뽑아야 한다.

그래서 i--를 해주어 for문의 증감식으로 올라갈 때 다시 0으로 올라가 i++을 만나 1의 자릿수의 숫자를 뽑도록 조치해준다.

	Scanner sc=new Scanner(System.in);
	int guip; // 몇장 살껀지	
	int[]lotto=new int[6];
		
	System.out.println("lotto는 몇장을 사실껀가요?");
	guip=sc.nextInt();
		
	// 예외조항
	if(guip<1) {
		System.out.println("***lotto 구입 장수가 부족합니다.***");
		return; // 메인함수 자체를 빠져나간다.
	}
		
	for(int n=0;n<guip;n++) {
		System.out.printf("\n%d회: ",n+1);			
		
		for(int i=0;i<lotto.length;i++) {
			// 1~45까지의 랜덤수 발생.	
			lotto[i]=(int)(Math.random()*45+1);
			
			// 중복처리
			for(int j=0;j<i;j++) {
				if(lotto[i]==lotto[j]) {
					i--; // 1. 잘못 뽑으면 일단 횟수를 1개 빼서 재시도.
					break;  //2. i++로 올라가서 같은 번지에 다시 값을 구하기 위해서.
				}
			}
		}
		// 오름차순 정렬.
		Arrays.sort(lotto);
	
		// 오름차순정렬(기분:처음~끝에서 2번째    비교:시작다음값~끝)
		for(int i=0;i<lotto.length-1;i++) {
			for(int j=1+i;j<lotto.length;j++) {
				if(lotto[i]>lotto[j]) {
					int temp=lotto[i];
					lotto[i]=lotto[j];
					lotto[j]=temp;
				}
			}
		}
	
		// 출력
		for(int i=0;i<lotto.length;i++) {
			System.out.printf("%5d",lotto[i]);
		}
} // 첫번째 for문 끝지점
profile
java를 잡아...... 하... 이게 맞나...

0개의 댓글