알고리즘/코딩도장
타노스의 핑거 스냅
개발계발게발
2021. 5. 28. 09:03
반응형
코딩도장 - 타노스의 핑거 스냅
import java.util.Arrays;
public class Tanos {
public static void main(String[] args) {
int[] people = {2, 3, 1, 6, 5, 7, 8};
int ran = people.length/2;
if(people.length%2==1) { //원소가 홀수라면
ran = (int) (Math.random()*2 + 1);// 1,2 절반 확률
if(ran%2==1) {
ran = (people.length/2)+1; // 1일때 배열초기화 값 저장
System.out.println("1일때");
}
else {
ran = (people.length/2); // 2일때 배열초기화 값 저장
System.out.println("2일때");
}
}
int[] half = new int[ran]; //half 배열 초기화
for(int i = 0; i < half.length; i++) {
int r = (int) (Math.random()*people.length);
if(people[r] !=0) { // 0이 아니라면
half[i] = people[r]; //half[i]에 people[i]값 저장
people[r] = 0; //저장된 people[r] 값 0으로
}else {
i--; //배열값이 0이면 다시 반복
}
}
System.out.println(Arrays.toString(people));
System.out.println(Arrays.toString(half));
}
}
https://codingdojang.com/scode/592
코딩도장
프로그래밍 문제풀이를 통해서 코딩 실력을 수련
codingdojang.com
반응형