학생들 중 사용자가 입력한 숫자만큼 랜덤으로 학생을 뽑는 프로그램을 만들어주세요.
내 풀이
String[] students = {"자바천재", "어피치", "라이언", "죠르디", "쿼카", "샐리", "브라운", "코니", "꿈돌이", "우디"}; int arr[]; System.out.println("희망 인원 입력>"); int num = Integer.parseInt(sc.nextLine()); arr = new int[num]; for(int i = 0; i < arr.length; i++){ arr[i] = (int)((Math.random())*studets.length); } //중복 방지 for (int i = 0; i < arr.length; i++) { boolean flag = false; for (int j = 0; j < arr.length - 1 - i; j++) { if(arr[j] == arr[j+1]){ flag = true; } } if(flag){ break; } System.out.print(students[arr[i]]+" "); }
선생님 풀이
Scanner sc = new Scanner(System.in); System.out.println("몇명?"); int count=Integer.parseInt(sc.nextLine()); String[]pick = new String[count]; //뽑은 사람을 저장할 배열 int pickCount = 0; //현재 몇명을 뽑았는지 카운트 하기 위해 변수생성 do{ //뽑고 나서야 그만둘지, 더할지 결정하기 때문에 int random = (int)(Math.random()*students.length); boolean flag = true; // 중복확인용 for(int i = 0; i < pick.length; i++){ if(students[random].equals(pick[i])){ // 랜덤으로 뽑은 사람과 이미 뽑힌 사람이 같으면 flag = false flag = false; } } if(flag){ //변수 값이 그대로 내려왔다 = 중복아니다 pick[pickCount++] = students[random];} //중복이 아닐 때 pick 배열에 이름 저장하기 }while(pickCount < count); System.out.println(Arrays.toString(pick));
Quiz 1
int money = (int)(Math.random()500)10;
int[]coin = {500, 100, 50, 10};
System.out.println("거스름돈 : "+ money);
거스름돈에 동전의 단위마다 몇개의 동전이 필요한지 출력해주세요.
ex) 거스름돈 : 2860원
500원 : 5개
100원 : 3개
50원 : 1개
10원 : 1개
for(int i = 0; i < coin.length; i++){
int count = money/coin[i];
money = money % coin[i];
System.out.println(coin[i] + "원 :" + count + "개");
}
패턴 반복 -> for문으로 바꾸기
count = money/coin[0]; //count : 필요한 동전의 개수
money = money % coin[0]; //500원으로 나누고 남은 나머지 금액
count = money / coin[1];
money = money % coin[1]; //100원으로 나누고 남은 나머지 금액
count = money / coin[2];
money = money % coin[2]; //50원으로 나누고 남은 나머지 금액
count = money / coin[3];
money = money % coin[3]; //10원으로 나누고 남은 나머지 금액
Quiz 2
1~5의 숫자가 발생한 횟수만큼 *을 사용해 그래프를 그려주세요.
int[] arr = new int[20];
for(int i = 0; i < arr.length; i++){
arr[i] = (int)(Math.random() * 5) + 1;
}
System.out.println(Arrays.toString(arr));
* 1 : ***3
* 2 : ****4
* 3 : **2
* 4 : *****5
* 5 : ******6
int[] count = new int[5]; for(int i = 0; i < arr.length; i++){ count[arr[i]-1]++; } for(int i = 0; i < count.length; i++){ System.out.print(i + 1 + ":"); for(int j = 0; j < count[i]; j++){ System.out.print("*"); } System.out.println(" " + count[i]);
Quiz 3
1~5사이의 랜덤한 값이 1개 저장된 배열에서 중복된 값이 제거된 배열을 만들어주세요.
int[] arr = new int[10];
for(int i = 0; i <arr.length; i++){
arr[i] = (int)(Math.random()*5)+1;
}
System.out.println(Arrays.toString(arr));
* [3,2,3,2,3,4,5,5,2,4]
* [3,2,4,5]
* 값이 들어있는 순서는 바뀌면 안됨!
내 풀이
int[]pickCount = new int[5]; int pick = 0; for(int i = 0; i <arr.length; i++){ boolean flag = false; for(int j = 0; j <pickCount.length; j++){ if(arr[i]==pickCount[j]){ flag = true; } }if(!flag){ pickCount[pick++]=arr[i]; } } int[]value = new int[pick]; for(int i = 0; i <value.length; i++){ value[i] = pickCount[i]; } System.out.println(pick); System.out.println(Arrays.toString(value));
선생님 풀이
int[]temp = new int[5]; int count = 0; for(int i = 0; i <arr.length; i++){ boolean flag = false; for(int j = 0; j <temp.length; j++){ if(arr[i]==temp[j]){ flag = true; } } if(flag==false){ temp[count++] = arr[i]; } } System.out.println(Arrays.toString(temp)); //배열 길이 맞추기 int[] result = new int[count]; for(int i = 0; i <result.length; i++){ result[i] = temp[i]; } System.out.println(Arrays.toString(result));