여러개의 생일 데이터가 주어졌을 때 생일이 같은 쌍 수동으로 결정하는 방법
2. Manually determination
생일 날짜에 학생을 기록해둠.
같은 날짜에 기록되어 있는 학생끼리 쌍을 지어줌
3. Develop my Algorithm
슈도코드
학생 class(String name, int id, int month, int date)
생일 class(vector student)
Student myList [13][32];
Student s<- name, id, month, date
myList[s.month][s.date].add(s) //학생의 생일에 해당하는 배열에 학생을 추가해준다.
자바 코드
import java.util.*;
import java.io.*;
class Student{
String name;
int id;
int month;
int date;
Student(String name, int id, int month, int date){
this.name=name;
this.id=id;
this.month=month;
this.date=date;
}
}
class Birthday{
Vector<Student> v;
Birthday(){
v=new Vector<Student>();
}
}
public class FindSameBirthday {
public static void main(String[] args) {
Birthday b[][]=new Birthday [13][32];
Student a=new Student("name",1,12,22);//학생 객체 생성
if(b[a.month][a.date]==null){//그 날에 생일인 학생을 처음 저장
b[a.month][a.date]=new Birthday();//객체 생성
}
b[a.month][a.date].v.add(a);//vector 추가
}
}
4. k명의 학생이 있을 때, 생일이 같은 학생이 적어도 2명이 존재할 확률
import java.util.Scanner;
public class ProbailitySameBirthday {
public static void main(String[] args) {
System.out.println("Please Enter the number of student");
Scanner sc = new Scanner(System.in);
int k=sc.nextInt();//학생 수
double result;
double np=1;
if(k>365)
result=1;
else {
//매치되지 않을 확률 np: (365*364*363*...*(365-k+1))/(365^k)
for(int i=0;i<k;i++) {
np=np*(double)(365-i)/365;
}
result=1-np;
}
System.out.println("result: "+ result);
}
}
5. 100명의 학생이 존재할 때 확률이 99.999임을 증명해라
슈도코드
p(n)= 1-(e)^(n^2/730)
e<- 오일러 상수
자바코드
public class ProbabilitySameBirthday2 {
public static void main(String[] args) {
System.out.println("Please Enter the number of student");
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();//학생 수
double e=Math.E;
double probability;
probability=1-Math.pow(e, -(n*n)/730);
System.out.println("result: "+probability);
}
}
댓글