JAVA를 JAVA라

[JAVA] 타노스의 배열 제거(다른 해답) 본문

JAVA/풀어봅시다 (eclipse)

[JAVA] 타노스의 배열 제거(다른 해답)

샛별KIM 2021. 5. 26. 16:28
public class ThanosTeach {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int ran = 0;

		int[] people = { 2, 3, 1, 6, 5, 7, 10 };
		int half = (int) (people.length / 2 + (Math.random() * 2));
		// 0 또는 1이 랜덤으로.. 즉 7일 때 3 또는 4 랜덤으로 죽이기

		System.out.println(Arrays.toString(people));

		for (int i = 0; i < half; i++) {
			int r = (int) (Math.random() * people.length);
			if (people[r] != 0) {
				people[r] = 0;
			} else {
				i--;
			}
		}
		for (int i = 0; i < people.length; i++) {

		if (people[i] != 0) {
			System.out.print(people[i] + ", ");
		}
	}

	System.out.println("");
	System.out.print(Arrays.toString(people));
	sc.close();
	}
}
Comments