김기헌
퀵 정렬(Quick Sort)
public class QuickSort {
public static void quickSort(int[] arr, int start, int end) {
if (start >= end) return;
int pivot = arr[(start+end)/2], left = start, right = end;
while (left <= right) {
while (arr[left] < pivot) left++;
while (arr[right] > pivot) right--;
if(left<=right){
int tmp=arr[left];
arr[left]=arr[right];
arr[right]=tmp;
left++;
right--;
}
}
quickSort(arr, start, right);
quickSort(arr, left, end);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[10];
for (int i = 0; i < 10; i++) {
arr[i] = sc.nextInt();
}
quickSort(arr, 0, arr.length - 1);
for (int i = 0; i < 10; i++) {
System.out.println(arr[i]);
}
}
}
Java
복사
DB Relation
Join
김상호
임학준
조국현
최아영
알고리즘
리스트 안의 모든 값 더하기
public class ListSum {
public static int sum(List<Integer> nums) {
if(nums.isEmpty()) return 0;
return nums.remove(nums.size() - 1) + sum(nums);
}
public static void main(String[] args) {
List<Integer> nums = new ArrayList<>();
nums.add(7);
nums.add(3);
nums.add(2);
nums.add(9);
System.out.println(sum(nums));
}
}
Java
복사
배열 안의 모든 값 더하기
public class ArrarySum {
public static int sum(int[] arr, int idx) {
if (l < 0) {
return 0;
}
return sum(arr, idx-1) + arr[idx];
}
public static void main(String[] args) {
int[] arr = {7, 3, 2, 9};
System.out.println(sum(arr, arr.length-1));
}
}
Java
복사
public class PrintStar {
public static void star(int n) {
if (n == 0) {
return;
}
System.out.print("*");
star(n-1);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
star(n);
}
}
Java
복사
public class RecursiveSum {
public static int sumNum(int n) {
if (n <= 0) {
return n;
}
return sumNum(n-1) + n;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(sumNum(n));
}
}
Java
복사
public class DigitsSum {
public static Long digit(Long n) {
if (n <= 0) {
return n;
}
return digit(n / 10) + n % 10;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Long n = sc.nextLong();
System.out.println(digit(n));
}
}
Java
복사
DataBase
테이블 생성
<book>
create table `db-exercies`.book
(
id int auto_increment,
name varchar(45) not null comment '책 제목',
publisher_id int null comment '출판사 id',
author_id int null comment '저자 id',
constraint book_pk
primary key (id)
);
SQL
복사
<author>
create table `db-exercies`.author
(
id int auto_increment,
name varchar(45) not null,
constraint author_pk
primary key (id)
)
comment '저자';
SQL
복사
<publisher>
create table `db-exercies`.publisher
(
id int auto_increment,
name varchar(50) not null comment '출판사 이름',
address varchar(200) null comment '주소',
constraint publisher_pk
primary key (id)
)
comment '출판사';
SQL
복사
join
id가 일치하는 것 조회
select * from book, author
where book.author_id = author.id;
SQL
복사
테이블 3개 join
select *
from book, author, publisher
where book.publisher_id = publisher.id
and book.author_id = author.id;
SQL
복사
필요한 컬럼만 추출
select book.name, author.name, publisher.name
from book, publisher, author
where book.publisher_id = publisher.id
and book.author_id = author.id;
SQL
복사