Skip to content

Commit

Permalink
feat: 알고리즘 문제풀이 5개
Browse files Browse the repository at this point in the history
feat: 알고리즘 문제풀이 5개
  • Loading branch information
dlwnsgus777 committed Dec 14, 2022
1 parent 1b7c8d8 commit 923ab40
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 0 deletions.
@@ -0,0 +1,39 @@
package baekjoon.basic;

import java.util.Scanner;

public class 최대공약수와최소공배수 {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

int a = sc.nextInt();
int b = sc.nextInt();

int gbc = gbc(a, b);
int lcm = lcm(a, b);

System.out.println(gbc);
System.out.println(lcm);
}

private static int gbc(int a, int b) {
if (a < b) {
int temp = a;
a = b;
b = temp;
}

while(b != 0) {
int r = a % b;
a = b;
b = r;
}

return a;
}

private static int lcm(int a, int b) {
return a * b / gbc(a, b);
}
}
32 changes: 32 additions & 0 deletions baekjoon/src/main/java/baekjoon/datastructure/카드2.java
@@ -0,0 +1,32 @@
package baekjoon.datastructure;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;

public class 카드2 {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
int n = Integer.parseInt(br.readLine());
Queue<Integer> que = new LinkedList<>();

for (int i = 1; i <= n; i++) {
que.offer(i);
}

while (que.size() != 1) {
que.poll();

que.offer(que.poll());
}

System.out.println(que.poll());


} catch (IOException e) {
e.printStackTrace();
}
}
}
34 changes: 34 additions & 0 deletions baekjoon/src/main/java/baekjoon/implementation/음계.java
@@ -0,0 +1,34 @@
package baekjoon.implementation;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class 음계 {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
String[] arr = br.readLine().split(" ");
String result = arr[0].equals("1") ? "ascending" : "descending";

for (int i = 1; i < 8; i++) {
int first = Integer.parseInt(arr[i - 1]);
int second = Integer.parseInt(arr[i]);

if (first + 1 == second) {
result = "ascending";
} else if (second + 1 == first) {

result = "descending";
} else {
result = "mixed";
break;
}
}

System.out.println(result);

} catch (IOException e) {
e.printStackTrace();
}
}
}
@@ -0,0 +1,42 @@
package baekjoon.implementation;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class 팰린드롬수 {

public static void main(String[] args) {
try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out))) {
String k = br.readLine();
while (!k.equals("0")) {
String[] arr = k.split("");
int i = 0;
int j = arr.length - 1;

while(i <= arr.length - 1 || j >= 0) {
if (i >= j) {
bw.write("yes\n");
break;
}

if (!arr[i].equals(arr[j])) {
bw.write("no\n");
break;
}

i++;
j--;
}
k = br.readLine();
}

bw.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
42 changes: 42 additions & 0 deletions baekjoon/src/main/java/baekjoon/sort/수찾기.java
@@ -0,0 +1,42 @@
package baekjoon.sort;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class 수찾기 {
public static void main(String[] args) {
try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
int n = Integer.parseInt(br.readLine());
Map<String, Integer> map = new HashMap<>();

String[] in = br.readLine().split(" ");

for (String s : in) {
map.put(s, 0);
}

int m = Integer.parseInt(br.readLine());
String[] inputMArray = br.readLine().split(" ");

List<String> inputMList = new ArrayList<>(Arrays.asList(inputMArray));

for(String num : inputMList) {
if (map.containsKey(num)) {
System.out.println("1");
} else {
System.out.println("0");
}
}


} catch (IOException e) {
e.printStackTrace();
}
}
}

0 comments on commit 923ab40

Please sign in to comment.