Skip to content
Open
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
54 changes: 54 additions & 0 deletions playcodonv/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package A0331;

import java.util.StringTokenizer;

public class Solution {

public static void main(String[] args) {
String[] id_list= {"con", "ryan"};
String[] report = {"ryan con", "ryan con", "ryan con", "ryan con"};
int[][] check = new int[id_list.length][id_list.length];
int k = 3;
int[] stop = new int[id_list.length];
int[] answer = new int[id_list.length];

for(int i = 0;i<report.length;i++) {
StringTokenizer st = new StringTokenizer(report[i]," ");
String one = st.nextToken();
String two = st.nextToken();
//System.out.println(one+ " "+two);
int police = 0; //신고한사람 idx
int thief = 0; //신고 당한사람 idx
for (int j = 0; j < id_list.length; j++) {
if(id_list[j].equals(one)) police = j;
if(id_list[j].equals(two)) thief = j;
}
check[police][thief] = 1;
}

for(int i = 0;i< id_list.length;i++) {
for(int j = 0;j< id_list.length;j++) {
if(check[j][i] == 1) stop[i] ++;
}
}
/*for(int i =0;i<stop.length;i++) {
System.out.print(stop[i]+" ");
}

System.out.println("");*/


for(int i = 0;i<id_list.length;i++) {
for(int j = 0;j<id_list.length;j++) {
if(stop[j] >= k && check[i][j] == 1) {
answer[i]++;
}
}
}
/*for(int i =0;i<stop.length;i++) {
System.out.print(answer[i]+" ");
}*/

}

}
50 changes: 50 additions & 0 deletions playcodonv/Solution2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package A0331;

import java.util.*;
import java.io.*;

public class Solution2 {


public static void main(String[] args) throws Exception{
int n = 1000000;
int k = 3;
String change="";
int answer = 0;
//진법변환

change = Integer.toString(n, k);
// change = "";
change += '0';

String sector = "";
for(int i = 0; i<change.length();i++) {
if(change.charAt(i) != '0') sector += change.charAt(i);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

String에 + 연산이 많아질거 같은 경우에는 StringBuilder를 사용하는 것을 권장합니다 !
image

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

좋아용~

else {
//Integer.parseInt(sector);
//System.out.println(sector);
int temp = Integer.parseInt(sector);
//System.out.println(temp);
sector = "0"; //null 대신에 이걸 넣어줘야함
if(isPrime(temp)) {
answer ++;
}
else continue;
}
}
System.out.println(answer);

}
static boolean isPrime(long number) {

if(number == 0) return false;
if(number == 1) return false;
if(number == 2) return true;
for(int i = 3; i<=Math.sqrt(number); i = i+2) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

매우 좋은 코드였습니다. 감사합니다 !

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

for문이 반복할 때마다 조건문을 확인하기 때문에, 매번 Math.sqrt() 함수가 실행됩니다
변수로 저장해서 사용하면 sqrt함수 실행을 한번만 하기 때문에 시간적으로 더 좋은 코드가 될 수 있을 거라 생각합니다~~!😺

if(number % i == 0) return false;
}
return true;
}


}