-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
665 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package day04; | ||
|
||
import java.util.Scanner; | ||
//10진수 정수값을 넘기면 2진수 문자열로 출력하는 함수를 재귀함수로 구현해보세요 | ||
public class BinaryNum { | ||
|
||
public static void binaryNum(int num) { | ||
if(num<=0) return; | ||
binaryNum(num/2); | ||
System.out.print(num%2); | ||
}//---------------------------------- | ||
|
||
public static void radixNum(int num, int radix) { | ||
if(num<=0) return; | ||
radixNum(num/radix, radix); | ||
int mod=num%radix; | ||
String str="0123456789ABCDEF"; | ||
System.out.print(str.charAt(mod)); | ||
}//-------------------------------- | ||
|
||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
System.out.println("정수를 입력하세요=>"); | ||
int num = sc.nextInt(); | ||
binaryNum(num); | ||
System.out.println(); | ||
|
||
System.out.println(num+"의 8진수 값: "); | ||
radixNum(num, 8); | ||
System.out.println(); | ||
|
||
System.out.println(num+"의 16진수 값: "); | ||
radixNum(num, 16); | ||
System.out.println(); | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package day04; | ||
//Comparator인터페이스를 상속받는 클래스를 inner class로 만들자 | ||
import java.util.*; | ||
public class Emp { | ||
private String name;//이름 | ||
private int height;//키 | ||
public Emp() { | ||
|
||
} | ||
public Emp(String n, int h) { | ||
name=n; | ||
height=h; | ||
} | ||
public String toString() { | ||
return "["+name+" : "+height+"cm]"; | ||
} | ||
public static final Comparator<Emp> HEIGHT_ORDER=new HeightOrderComparator(); | ||
//키 오름차순 정렬을 위한 Comparator클래스를 만들고 해당 객체를 생성하자 | ||
static class HeightOrderComparator implements Comparator<Emp>{ | ||
@Override | ||
public int compare(Emp e1, Emp e2) { | ||
/*if(e1.height==e2.height) return 0; | ||
else if(e1.height>e2.height) return 1; | ||
else return -1;*/ | ||
// return e1.height-e2.height; //오름차순 | ||
return e2.height-e1.height; //내림차순 | ||
} | ||
}//////////////// | ||
|
||
//이름 오름차순 정렬을 위한 Comparator | ||
public static final Comparator<Emp> NAME_ORDER=new NameOrderComparator(); | ||
|
||
static class NameOrderComparator implements Comparator<Emp>{ | ||
public int compare(Emp e1, Emp e2) { | ||
return e1.name.compareTo(e2.name); | ||
} | ||
} | ||
|
||
|
||
}////////////////////// |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package day04; | ||
/*n! | ||
1! : 1 | ||
2! : (1)x 2 | ||
3! : (1x2)x3 | ||
4! : (1x2x3)x4 | ||
5! : (1x2x3x4)x5 | ||
n! : (n-1)!xn | ||
여기서 1!=1 그리고 n!=nx(n-1)! 라는 팩토리얼 성질을 이용해서 프로그램을 만들어보자 | ||
*/ | ||
import java.util.*; | ||
public class Factorial { | ||
|
||
public static int factorial(int num) { | ||
if(num==1) return 1; | ||
String str=(num==1)?"=":"x"; | ||
System.out.print(num+str); | ||
return factorial(num-1); | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
System.out.println("정수를 입력하세요=>"); | ||
int num = sc.nextInt(); | ||
System.out.println(factorial(num)); | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package day04; | ||
|
||
import java.util.Scanner; | ||
|
||
public class Fibonacci { | ||
// 1 1 2 3 5 8 13 | ||
public static void fibo(int n) {//n은 항 | ||
int num1=1; | ||
int num2=1; | ||
int num3=0; | ||
System.out.println(num1+" "+num2); | ||
for(int i=0; i<n-2; i++) { | ||
num3=num1+num2; | ||
System.out.println(num3+" "); | ||
num1=num2; | ||
num2=num3; | ||
} | ||
} | ||
//피보나치 수열을 재귀함수를 이용해서 구현하기 | ||
public static int fibo2(int n) { | ||
if(n==1) return 1; | ||
if(n==2) return 1; | ||
return fibo2(n-2)+fibo2(n-1); | ||
//n번째 항의 앞과 앞의 앞을 더해 반환 | ||
}//----------------------------------------- | ||
|
||
static int [] arr; | ||
public static int fibo3(int n) { | ||
if(n==1) return arr[n]=1; | ||
else if(n==2) return arr[n]=1; | ||
else return arr[n]=fibo3(n-2)+fibo3(n-1); | ||
} | ||
|
||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
System.out.println("정수 입력: "); | ||
int n= sc.nextInt(); | ||
// fibo(n); | ||
for(int i=1; i<=n; i++) { | ||
System.out.print(fibo2(i)+", "); | ||
} | ||
System.out.println("\n------------------"); | ||
arr=new int[n+1]; | ||
fibo3(n); | ||
|
||
for(int a: arr) { | ||
if(a==0) continue; | ||
System.out.print(a+", "); | ||
} | ||
} | ||
|
||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package day04; | ||
|
||
public class MyArrayList<E> { | ||
|
||
private Object[]data; | ||
private int capacity; | ||
private int index; | ||
|
||
public MyArrayList() { | ||
this(5); | ||
} | ||
|
||
public MyArrayList(int initCapa) { | ||
this.capacity=initCapa; | ||
this.data=new Object[capacity]; | ||
this.index=0; | ||
|
||
} | ||
//데이터 추가 | ||
public void add(E obj) { | ||
if(index>=capacity-1) {//데이터가 꽉 찼다면 => 두배로 증가시키자 | ||
doubling(); | ||
} | ||
data[index++]=obj; | ||
}//--------------------- | ||
private void doubling() { | ||
this.capacity= this.capacity*2;//2배로 | ||
Object []newData=new Object[this.capacity]; | ||
//기존 배열에 저장된 데이터를 새로운 배열로 옮기자 | ||
for(int i=0; i<data.length; i++) { | ||
newData[i]=data[i]; | ||
} | ||
this.data=newData; | ||
//멤버변수data에 새로운 배열의 주소값을 전달한다 | ||
System.out.printf("index: %d, capacity: %d, size: %d%n",index, capacity, this.size()); | ||
}//---------------------------------------- | ||
//리스트에 담긴 데이터 개수를 반환 | ||
public int size() { | ||
return index; | ||
}//---------------------------- | ||
//데이터 꺼내기 | ||
public E get(int i){ | ||
if(i>index-1) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
}else if(i<0) { | ||
throw new RuntimeException("Negative Index Value"); | ||
} | ||
Object val=data[i]; | ||
return (E)val; | ||
}//---------------------------- | ||
//특정 인덱스의 데이터 삭제 | ||
public void remove(int i) { | ||
if(i>index-1) { | ||
throw new ArrayIndexOutOfBoundsException(); | ||
}else if(i<0) { | ||
throw new RuntimeException("Negative Index Value"); | ||
} | ||
for(int k=i; k<this.data.length-1; k++) { | ||
data[k]=data[k+1]; //뒤에 있는 요소를 옆으로 이동시킨다 | ||
} | ||
this.index--; | ||
} | ||
|
||
}////////////////////////////////////////////////////////// | ||
|
||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package day04; | ||
|
||
public class MyArrayListTest { | ||
|
||
public static void main(String[] args) { | ||
//MyArrayList를 생성해서 | ||
//정수형 객체를 몇개 저장한 후에 반복문 이용해서 한꺼번에 출력하기 | ||
|
||
MyArrayList<Integer> arr = new MyArrayList();//초기용량 5 | ||
arr.add(10); | ||
arr.add(20); | ||
arr.add(30); | ||
arr.add(40); | ||
System.out.println("arr.size(): "+arr.size()); | ||
arr.add(50); | ||
arr.add(60); | ||
System.out.println("arr.size(): "+arr.size()); | ||
|
||
for(int i=0; i<arr.size(); i++) { | ||
Integer ival = arr.get(i); | ||
System.out.println(ival); | ||
} | ||
// arr.remove(7); | ||
arr.remove(1); | ||
System.out.println("--삭제후-----------------------"); | ||
for(int i=0; i<arr.size(); i++) { | ||
Integer ival=arr.get(i); | ||
System.out.println(ival); | ||
} | ||
|
||
MyArrayList<String> arr2 = new MyArrayList<>(); | ||
|
||
arr2.add("Hello"); | ||
arr2.add("Hi"); | ||
|
||
String str=arr2.get(1); | ||
System.out.println(str); | ||
|
||
|
||
} | ||
|
||
} |
Oops, something went wrong.