Last active
July 7, 2023 17:06
-
-
Save rorrorome/54c9a9cbc8885ce720c927babd81f177 to your computer and use it in GitHub Desktop.
투표
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * 제로베이스 백엔드 스쿨 15기 | |
| * 한새롬 | |
| * 미니과제 6. 가상 선거 및 당선 시뮬레이션 프로그램 | |
| * | |
| * 조건문 및 반복문과 배열(or 컬렉션)을 통한 당선 시뮬레이션 로직 작성 | |
| * 전체 투표수와 후보자를 입력받아서 그 결과를 미리 확인 | |
| * 1. 총 투표를 진행할 투표수를 입력 받음 | |
| * 2. 선거를 진행할 후보자 수를 입력 받고, 이에 대한 이름을 입력 받음 | |
| * 3. 각 입력받은 후보자는 순서대로 기호1, 기호2, 기호3… 형식으로 기호번호 부여함 | |
| * 4. 각 투표수의 결과는 선거를 진행할 후보자를 동일한 비율로 랜덤하게 발생 | |
| * 5. 임의번호는 Random함수의 nextInt()함수를 통해서 생성 | |
| * 6. 1표에 대한 투표한 결과에 대해서 투표자와 이에 대한 결과를 화면 출력해야 함 | |
| * 아래 내용은 전제조건으로 진행 | |
| * - 투표수는 1 ~ 10000 사이의 값을 입력하며, 그외 값 입력에 대한 예외는 없다고 가정함. | |
| * - 후보자 인원은 2 ~ 10 사이의 값을 입력받으면, 그외 값 입력에 대한 예외는 없다고 가정함. | |
| * - 후보자이름은 한글로 입력하며, 10자 미만으로 입력함. (역시, 그외 입력에 대한 예외는 없다고 가정함.) | |
| */ | |
| import java.util.Scanner; | |
| import java.util.Random; | |
| public class MiniProject06 { | |
| public static void main(String[] args) { | |
| Scanner sc = new Scanner(System.in); | |
| System.out.print("총 진행할 투표수를 입력해 주세요."); | |
| int people = sc.nextInt(); | |
| System.out.print("가상 선거를 진행할 후보자 인원을 입력해 주세요."); | |
| int num = sc.nextInt(); | |
| String[] array = new String[num]; | |
| for (int i = 1; i <= num; i++) { | |
| System.out.print(i + "번째 후보자 이름을 입력해 주세요."); | |
| array[i - 1] = sc.next(); | |
| } | |
| System.out.println(); | |
| // people개의 원소를 가진 배열 random에 (array의 원소)의 번호들을 랜덤하게 넣음 | |
| int[] random = new int[people]; | |
| Random rd = new Random(); | |
| for (int j = 0; j < people; j++) { | |
| random[j] = rd.nextInt(num); | |
| } | |
| int[] count = new int[num]; | |
| double[] get = new double[num]; | |
| for (int m = 1; m <= people; m++) { | |
| // 투표진행률 | |
| double progress = (double) m * 100 / people; | |
| // 득표수 = random 배열에서 후보의 기호-1번째 값을 가진 수의 값 (random의 0~3번째까지 기호1번의 득표수는 0이 나온 횟수) | |
| count[random[m - 1]]++; | |
| System.out.printf("[투표진행률]: %.2f%%, %d명 투표 => %s\n", progress, m, array[random[m - 1]]); | |
| for (int n = 0; n < num; n++) { | |
| // 득표율(백분율)(기호1번의 득표율 = get배열의 0번째 원소) | |
| get[n] = count[n] * 100 / people; | |
| System.out.printf("[기호:%d]\t %s:\t%.2f%%\t(투표수: %d)\n", n + 1, array[n], get[n], count[n]); | |
| } | |
| System.out.println(); | |
| } | |
| for (int x = 0; x < num; x++) { | |
| for (int y = 0; y < x; y++) { | |
| if (count[x] == count[y]) { | |
| System.out.println("동률 발생 재투표 진행"); | |
| } | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment