Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions chris-an/home/3.11/Boj_10872.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import java.util.Scanner;

public class Boj_10872 {

static int value = 1;
public static int factorial(int n) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

재귀로 구현할 때

public static int factorial(int n) {
    if (n == 1) return n;
    return factorial(n - 1) * n;
}

이렇게 줄여도 될 것 같습니다!

if (n == 0) return value;
value *= n ;
return factorial(n-1);
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(factorial(sc.nextInt()));
}
}
44 changes: 44 additions & 0 deletions chris-an/home/3.11/Boj_1676.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


public class Boj_1676 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int cnt = 0;
for (int i = 5; i <= n ; i *= 5) {
cnt += n / i;
}

System.out.println(cnt);
}
}

/*

public class Main {

static int value = 1;

public static int factorial(int n) {
if (n == 0) return value;
value *= n;
return factorial(n - 1);
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = factorial(sc.nextInt());
char [] ch = Integer.toString(N).toCharArray();

int cnt = 0;
for (int i = ch.length-1; i > 0; i--) {
if (ch[i] == '0') cnt++;
else break;
}
System.out.println(cnt);
}
}
*/
91 changes: 91 additions & 0 deletions chris-an/home/3.11/Boj_6588.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import java.io.*;
import java.util.Arrays;


public class Boj_6588 {
static boolean [] targetNumbers;
public static void isPrime() {
targetNumbers = new boolean[1000001];

Arrays.fill(targetNumbers, true);
// for(int i=2;i<=N;i++) {
// targetNumbers[i] = true;
// }
for (int i = 2; i<=1000000; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

에라토스테네스의 체를 구현할 때 두 개의 이중 루프를 돌 때마다 i, j가 각각 2부터 100만까지 돌 필요없이
i는 2부터 100만의 제곱근까지만, j는 i * i 부터 100만까지 돌면 시간 복잡도를 줄일 수 있습니다.

for (int i = 2; i * i <= n; i++) {
    if (!primes[i]) {
        for (int j = i * i; j <= n ; j += i) {
            primes[j] = true;
        }
    }
}

이런식으로요! 에라토스테네스의 체 구현 방법에 대해서 검색해보시면 도움이 되실거에요 :)

Copy link
Member Author

@CHRIS-AN CHRIS-AN Mar 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

구현 방식이 다르고, 배수를 확인하는 건 같은 로직이라
같은 '에라토스테네스 의 체' 로 알고있습니다 ! 혹시 몰라 지훈님이 말하는 방식으로 변경을 해서 복잡도 확인해보니깐 큰 차이는 없었습니다 !
Screen Shot 2022-03-12 at 7 27 10 AM

아래가 제 코드고, 위는 지훈님 구현방식입니다!
피드백 감사합니다😊

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오....저는 배수를 확인할때 100만 * 100만 이중 반복문을 돌 필요없이 첫 반복문에서부터 제곱근까지만 돌면 시간복잡도를 줄일 수 있다고 생각했는데 차이가 없네요! 다시한번 공부해봐야겠습니다. 비교 감사합니다~^^

// 이미 확인된 건 넘기기
if (!targetNumbers[i]) continue;
// 배수 출발.
for (int j = i * 2; j <= 1000000; j += i ) targetNumbers[j] = false;
}
}
public static void main(String[] args) throws IOException {


BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
isPrime();
while (true) {
int n = Integer.parseInt(br.readLine());
if (n == 0) break;

int primeNum = Integer.MIN_VALUE;
for (int i = 2; i <= n/2; i++) {
if (targetNumbers[i] && targetNumbers[n-i]) {
primeNum = i;
break;
}
}

if (primeNum > 0) bw.write(n + " = " + primeNum + " + " + (n-primeNum) + "\n");
else bw.write("Goldbach's conjecture is wrong.\n");
}
bw.flush();
bw.close();
br.close();
}
}


/*

시간 초과.

public class Main {
public static String isPrime(int n) {
ArrayList<Integer> list = new ArrayList<>();
boolean [] prime = new boolean[n+1];
Arrays.fill(prime, true);
prime[0] = prime[1] = false;
for (int i = 2; i <= Math.sqrt(prime.length); i++) {
if (prime[i]) {
for (int j = i*i; j <= n; j+=i) prime[j] = false;
}
}
for (int i = 2; i < n; i++) {
if (true == prime[i]) {
list.add(i);
}
}
for (int i = 0; i < list.size(); i++) {
for (int k = i; k < list.size(); k++) {
if (list.get(i)+list.get(k) == n) {
return n + " = " + list.get(i) + " + " + list.get(k);
}
}
}
return "Goldbach's conjecture is wrong.";
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int stop = 1;
while (stop != 0) {
stop = Integer.parseInt(br.readLine());
if (stop!=0) {
System.out.println(isPrime(stop));
}
}
}
}

*/
2 changes: 2 additions & 0 deletions chris-an/office/3.08/Programmers_42840.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public class Programmers_42840 {
}
51 changes: 51 additions & 0 deletions chris-an/office/3.10/Programmers_42839.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashSet;

public class Programmers_42839 {

static HashSet<String> hs = new HashSet<>();

public static void recur(String candidate, String remain) {
// base case 라고 불림.
if (!candidate.equals("")) hs.add(candidate);
for (int i = 0; i < remain.length(); i++){
recur(candidate + remain.charAt(i), remain.substring(0, i) + remain.substring(i+1));
}
}

public static boolean isPrime(int n) {
// 0, 1 는 제외
if (n < 2) return false;
// true = 소수, false = 합성수
boolean [] prime = new boolean[n+1];
Arrays.fill(prime, true);

prime[0] = prime[1] = false;
for (int i = 2; i*i <= n; i++) {
// 소수가 아니라면?
if (prime[i]) {
for (int j = i*i; j <= n; j+=i) prime[j] = false;
}
}
return prime[n];
}

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();

recur("", str);
int cnt = 0;

for (String s : hs) {
if (isPrime(Integer.parseInt(s)))
cnt++;
}

System.out.println(cnt);

}
}
38 changes: 38 additions & 0 deletions chris-an/office/3.10/Programmers_42842.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Programmers_42842 {

public static int [] solution(int brown, int yellow) {
/*
int a = brown/2 + 2;
int b = yellow + brown;

double alpha = (a+Math.sqrt(a*a - 4*b)) / 2;
double beta = a - alpha;

int [] answer = {(int) alpha, (int) beta};
*/

int[] answer = new int[2];

for(int i = 1; i <= yellow; i++) {
if(yellow % i == 0) {
if(2 * i + (2 * yellow / i) == brown - 4) {
answer[0] = i + 2;
answer[1] = yellow / i + 2;
}
}
}
return answer;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String [] ch = br.readLine().split(" ");
int b = Integer.parseInt(ch[0]);
int y = Integer.parseInt(ch[1]);

System.out.println(solution(b,y)[0] +" " + solution(b,y)[1]);
}
}
Binary file added chris-an/out/production/chris-an/Boj_10430.class
Binary file not shown.
Binary file added chris-an/out/production/chris-an/Boj_10809.class
Binary file not shown.
Binary file added chris-an/out/production/chris-an/Boj_10820.class
Binary file not shown.
Binary file added chris-an/out/production/chris-an/Boj_10824.class
Binary file not shown.
Binary file added chris-an/out/production/chris-an/Boj_11655.class
Binary file not shown.
Binary file added chris-an/out/production/chris-an/Boj_11656.class
Binary file not shown.
Binary file added chris-an/out/production/chris-an/Boj_1929.class
Binary file not shown.
Binary file added chris-an/out/production/chris-an/Boj_1934.class
Binary file not shown.
Binary file added chris-an/out/production/chris-an/Boj_1978.class
Binary file not shown.
Binary file added chris-an/out/production/chris-an/Boj_2609.class
Binary file not shown.
Binary file added chris-an/out/production/chris-an/Boj_2743.class
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.