-
Notifications
You must be signed in to change notification settings - Fork 0
1주차 알고리즘 스터디 문제풀이 코드 - 김채원 #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
The head ref may contain hidden characters: "be/b/\uAE40\uCC44\uC6D0_working"
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package baekjoon; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.StringTokenizer; | ||
|
||
class BJ_13567 { | ||
|
||
int mapSize, cmdCnt, curX, curY, dir; | ||
int dx[] = { 1, 0, -1, 0 }, dy[] = { 0, 1, 0, -1 }; | ||
|
||
void solve() { | ||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
try { | ||
StringTokenizer st = new StringTokenizer(br.readLine()); | ||
mapSize = Integer.parseInt(st.nextToken()); | ||
cmdCnt = Integer.parseInt(st.nextToken()); | ||
for (int i = 0; i < cmdCnt; i++) { | ||
st = new StringTokenizer(br.readLine()); | ||
String cmd = st.nextToken(); | ||
int dis = Integer.parseInt(st.nextToken()); | ||
if (cmd.equals("MOVE")) { | ||
curX += dx[dir] * dis; | ||
curY += dy[dir] * dis; | ||
} else if (cmd.equals("TURN")) { | ||
if (dis == 0) dir = (dir + 1) % 4; | ||
else dir = (dir + 3) % 4; | ||
} | ||
if (curX < 0 || curX >= mapSize || curY < 0 || curY >= mapSize) { | ||
System.out.println(-1); | ||
return; | ||
} | ||
} | ||
System.out.println(curX + " " + curY); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
Comment on lines
+36
to
+38
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PS 환경에서 IOException이 발생할 일은 거의 없을테니 메서드에 throws만 해주셔도 괜찮을 것 같아요 |
||
} | ||
|
||
public static void main(String[] args) { | ||
new BJ_13567().solve(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package baekjoon; | ||
|
||
import java.util.*; | ||
|
||
public class BJ_14467 { | ||
static ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>(); | ||
|
||
public static void main (String[]args){ | ||
Scanner scanner = new Scanner(System.in); | ||
int n = scanner.nextInt(); | ||
int sum = 0; | ||
|
||
for(int i=0; i<101; i++){ | ||
list.add(new ArrayList<Integer>()); | ||
} | ||
|
||
for(int i=0; i<n; i++){ | ||
int num = scanner.nextInt(); | ||
int dir = scanner.nextInt(); | ||
|
||
list.get(num).add(dir); | ||
} | ||
|
||
for(int i=0; i<101; i++){ | ||
if(!(list.get(i).size() <= 1)){ | ||
int k = list.get(i).get(0); | ||
for(int j=1; j<list.get(i).size(); j++){ | ||
if(k != list.get(i).get(j)){ | ||
k = list.get(i).get(j); | ||
sum++; | ||
} | ||
} | ||
} | ||
} | ||
Comment on lines
+24
to
+34
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 부분 알고리즘이 조금 이해가 안가는데 설명해주실 수 있나요?😮 |
||
|
||
System.out.print(sum); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package baekjoon; | ||
|
||
import java.io.*; | ||
import java.util.Comparator; | ||
import java.util.PriorityQueue; | ||
|
||
public class BJ_2870 { | ||
|
||
public static void main(String[] args) throws IOException { | ||
// TODO Auto-generated method stub | ||
BufferedReader br = new BufferedReader( new InputStreamReader( System.in ) ); | ||
|
||
int N = Integer.parseInt(br.readLine()); | ||
PriorityQueue<String> pq = new PriorityQueue<String>(new ASC_2870()); | ||
String tmp; | ||
char cTmp; | ||
String save; | ||
for(int i = 0 ; i < N ; i++) { | ||
tmp = br.readLine(); | ||
for(int j = 0 ; j < tmp.length() ; j++) { | ||
cTmp = tmp.charAt(j); | ||
save = ""; | ||
boolean check = false; | ||
while(cTmp >= '0' && cTmp <= '9') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 어제 리뷰에서 나왔던 것처럼 Character.isDigit 메서드를 활용하면 좋을 것 같네요 |
||
if(save.length() == 0 && cTmp == '0') { | ||
save = "0"; | ||
check = true; | ||
} | ||
else { | ||
// 첫 번째 값이 아닌 경우. 0 ~ 9가 다 가능 | ||
if(check) { // 첫 번째 값이 0인 경우. | ||
if(cTmp != '0') { // 첫 번째가 0이나, 두 번재는 0이 아니다. | ||
save = ""; | ||
save += cTmp; // 두 번째 값을 첫 번째로 사용. | ||
check = false; | ||
} | ||
// 첫 번째와 두 번째가 모두 0이다. 이는 0 그대로. > 아무것도 하지 않아도 됨 | ||
} | ||
else // 첫 번째 값이지만 0이 아닌 경우 어떤 값이 들어와도 상관 없음. | ||
save += cTmp; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 문자열 concatenation 연산을 수행할 때는 '+' 연산자 대신 StringBuilder나 StringBuffer를 쓰는 게 좋을 것 같습니다! |
||
} | ||
j++; | ||
if(j >= tmp.length()) | ||
break; | ||
cTmp = tmp.charAt(j); | ||
} | ||
if(save.length() == 0) | ||
continue; | ||
pq.add(save); | ||
} | ||
} | ||
|
||
|
||
while(!pq.isEmpty()) { | ||
String now = pq.poll(); | ||
System.out.println(now); | ||
} | ||
} | ||
} | ||
class ASC_2870 implements Comparator<String>{ | ||
|
||
public int compare(String v1, String v2) { | ||
if(v1.length() == v2.length()) | ||
return v1.compareTo(v2) < 0 ? -1 : 1; | ||
else | ||
return v1.length() - v2.length() < 0 ? -1 : 1; | ||
Comment on lines
+63
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. compareTo 메서드는 음수, 0, 양수 세 가지 분류로만 기준을 나누기 때문에 삼항 연산자로 변환할 필요 없이 그대로 반환해도 좋을 것 같아요! |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package baekjoon; | ||
|
||
import java.util.*; | ||
/* | ||
* 비밀번호 발음하기 | ||
* | ||
* 1. 모음(a,e,i,o,u) 하나를 반드시 포함하여야 한다. | ||
* 2. 모음이 3개 혹은 자음이 3개 연속으로 오면 안 된다. | ||
* 3. 같은 글자가 연속적으로 두번 오면 안되나, ee 와 oo는 허용한다. | ||
* | ||
* */ | ||
public class BJ_4659 { | ||
static final char[] vowels = {'a' ,'e' ,'i' ,'o' , 'u'}; | ||
public static void main(String[] args) { | ||
Scanner sc = new Scanner(System.in); | ||
while(true) { | ||
String word = sc.nextLine(); | ||
StringBuilder ans = new StringBuilder(""); | ||
|
||
if(word.equals("end")) break; | ||
|
||
ans.append("<").append(word).append("> "); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 특정 형식의 문자열을 만드는 거라면 String.format 처럼 포매팅을 해 보는것도 좋을 것 같습니다! |
||
|
||
if(!check(word)) { | ||
ans.append("is not acceptable."); | ||
}else { | ||
ans.append("is acceptable."); | ||
} | ||
System.out.println(ans.toString()); | ||
} | ||
} | ||
|
||
private static boolean check(String word) { | ||
boolean hasVowel = false; | ||
int vowel = 0, consonant = 0; | ||
char pre = ' '; | ||
for(int i =0, l = word.length(); i<l; i++) { | ||
boolean isVowel = false; | ||
// 1. 모음(a,e,i,o,u) 하나를 반드시 포함하여야 한다. | ||
for(int j = 0 ; j < 5 ; j++) { | ||
if(vowels[j]==word.charAt(i)) { | ||
isVowel = true; // 현재 모음임을 표시 | ||
hasVowel = true; // 모음을 포함함을 표시 | ||
break; | ||
} | ||
} | ||
// 2. 모음이 3개 혹은 자음이 3개 연속으로 오면 안 된다. | ||
if(isVowel) { // 모음인 경우 | ||
vowel++; | ||
consonant = 0; | ||
}else { // 자음인 경우 | ||
consonant++; | ||
vowel = 0; | ||
} | ||
if(vowel>=3 || consonant>=3) return false; | ||
|
||
// 3. 같은 글자가 연속적으로 두번 오면 안되나, ee 와 oo는 허용한다. | ||
if(pre == word.charAt(i)&& pre != 'e' && pre != 'o') return false; | ||
pre = word.charAt(i); | ||
} | ||
if(hasVowel)return true; | ||
return false; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package baekjoon; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.util.StringTokenizer; | ||
|
||
|
||
public class BJ_5635 { | ||
|
||
private String name; | ||
private String elderName; | ||
private String olderName; | ||
|
||
private int minDayOfAge,maxDayOfAge=0; | ||
private int minMonthOfAge,maxMonthOfAge=0; | ||
private int minYearOfAge,maxYearOfAge =0; | ||
Comment on lines
+15
to
+17
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 자바 int 기본값은 0으로 초기화되기 때문에 일일히 안 해주셔도 될 것 같습니다! |
||
|
||
|
||
public static void main(String[] args) throws IOException { | ||
|
||
BJ_5635 main = new BJ_5635(); | ||
main.go(); | ||
|
||
} | ||
|
||
public void go() throws IOException{ | ||
|
||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | ||
int n = Integer.parseInt(br.readLine()); | ||
StringTokenizer st; | ||
int[] info = new int[3]; | ||
|
||
st = new StringTokenizer(br.readLine(), " "); | ||
|
||
olderName = st.nextToken(); | ||
elderName = olderName; | ||
|
||
minDayOfAge = Integer.parseInt(st.nextToken()); | ||
minMonthOfAge = Integer.parseInt(st.nextToken()); | ||
minYearOfAge = Integer.parseInt(st.nextToken()); | ||
|
||
maxDayOfAge = minDayOfAge; | ||
maxMonthOfAge = minMonthOfAge; | ||
maxYearOfAge = minYearOfAge; | ||
|
||
while(n > 1){ | ||
|
||
st = new StringTokenizer(br.readLine(), " "); | ||
name = st.nextToken(); | ||
|
||
for(int i=0; i<3; ++i){ | ||
info[i]=Integer.parseInt(st.nextToken()); | ||
} | ||
|
||
elderName = findMinAge(name, info); | ||
olderName = findMaxAge(name, info); | ||
Comment on lines
+56
to
+57
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 생년월일을 한번에 다 모아놓고 정렬하는 게 아니라 매번 받을 때마다 업데이트하는 방식이군요!! 좋은 방법인 것 같아요 |
||
n--; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
System.out.println(elderName); | ||
System.out.println(olderName); | ||
|
||
} | ||
//나이 어린 사람 | ||
public String findMinAge(String name, int[] info){ | ||
|
||
if(minYearOfAge > info[2]){ | ||
return elderName; | ||
|
||
}else if(minYearOfAge == info[2]){ | ||
if(minMonthOfAge > info[1]){ | ||
return elderName; | ||
|
||
}else if(minMonthOfAge == info[1]){ | ||
if(minDayOfAge > info[0]){ | ||
return elderName; | ||
}else{ | ||
changeMinValue(info); | ||
return name; | ||
} | ||
}else{ | ||
changeMinValue(info); | ||
return name; | ||
} | ||
}else{ | ||
changeMinValue(info); | ||
return name; | ||
} | ||
} | ||
|
||
//나이 많은 사람 | ||
public String findMaxAge(String name, int[] info){ | ||
|
||
if(maxYearOfAge < info[2]){ | ||
return olderName; | ||
|
||
}else if(maxYearOfAge == info[2]){ | ||
if(maxMonthOfAge < info[1]){ | ||
return olderName; | ||
|
||
}else if(maxMonthOfAge == info[1]){ | ||
if(maxDayOfAge < info[0]){ | ||
return olderName; | ||
}else{ | ||
changeMaxValue(info); | ||
return name; | ||
} | ||
}else{ | ||
changeMaxValue(info); | ||
return name; | ||
} | ||
}else{ | ||
changeMaxValue(info); | ||
return name; | ||
} | ||
} | ||
|
||
public void changeMinValue(int[] info){ | ||
|
||
this.minYearOfAge =info[2]; | ||
this.minMonthOfAge = info[1]; | ||
this.minDayOfAge =info[0]; | ||
|
||
} | ||
|
||
public void changeMaxValue(int[] info){ | ||
|
||
this.maxYearOfAge =info[2]; | ||
this.maxMonthOfAge = info[1]; | ||
this.maxDayOfAge =info[0]; | ||
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package baekjoon; | ||
|
||
import java.util.*; | ||
public class BJ_7785{ | ||
public static void main(String[] args) { | ||
int N = 0; | ||
HashMap<String, String> hash=new HashMap<>(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 정렬을 위해서라면 TreeMap을 사용해 보는 것도 괜찮을 것 같습니다! |
||
Scanner in=new Scanner(System.in); | ||
N=in.nextInt(); | ||
String[][] map=new String[N][2]; | ||
|
||
for(int i=0;i<N;i++) { | ||
map[i][0]=in.next(); | ||
map[i][1]=in.next(); | ||
hash.put(map[i][0],map[i][1]); | ||
} | ||
|
||
ArrayList<String> arr=new ArrayList<>(); | ||
|
||
for(String str: hash.keySet()) | ||
if(hash.get(str).equals("enter")) arr.add(str); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 별로 상관없는 얘기일 수 있지만 두 문자열을 비교할 때 |
||
|
||
|
||
Collections.sort(arr, Collections.reverseOrder()); | ||
|
||
|
||
for(String str:arr) | ||
System.out.println(str); | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는 dis 만큼 1을 더하거나 빼는 반복문을 돌렸는데 이걸 보니 굳이 그럴 필요가 없었네요 ㅋㅋㅋ