평범한 연구소

[JAVA] 랜덤으로 조 짜기 본문

JAVA/알고리즘 공부

[JAVA] 랜덤으로 조 짜기

soyeonisgood 2022. 8. 2. 09:30

 

package ch12.unit7;

import java.util.Arrays;
import java.util.Collections;


public class Ex03 {
	
	// 21명 -> 4조 : 6,5,5,5
	public static void main(String[] args) {
		String[] aa = new String[] {"a","b","c","d","e","f","g","h","i","j",
			"k","l","m","n","o","p","q","r","s","t","u"};
		String[][] group = null;
		
		int cnt = 4; // 그룹 수
		int inwon = aa.length/4==0 ? aa.length/cnt: aa.length/cnt+1; // 그룹 별 최대 인원수
		
		group = new String[cnt][inwon];
		
		Collections.shuffle(Arrays.asList(aa)); // 무작위 섞기
		
		int n = 0;
		
		gogo:
		for(int col=0; col<inwon; col++) {
			for(int row=0; row<cnt; row++) {
				group[row][col] = aa[n++];
				if(n>=aa.length) {
					break gogo;
				}
			}
		}
		
		for(int i=0; i<cnt; i++) {
			System.out.println((i+1) + "조: ");
			for(int j=0; j<inwon; j++) {
				if(group[i][j]!=null) {
					System.out.print(group[i][j]+" ");
				}
			}
			System.out.println();
		}
		

	}

}