Skip to content

Commit 45932d7

Browse files
authored
Merge pull request #4 from algorithm-cote-study/yeji
PR : 2주차 문제 풀이
2 parents 82669c5 + a3441bb commit 45932d7

21 files changed

+743
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package sgyj.inflearn.study.week2;
2+
3+
import java.util.Scanner;
4+
5+
public class Solution11 {
6+
7+
public String solution(String s){
8+
String answer="";
9+
s = s+ " ";
10+
int cnt = 1;
11+
for(int i=0; i<s.length()-1; i++){
12+
if(s.charAt( i ) == s.charAt( i+1 )) cnt++;
13+
else{
14+
answer += s.charAt( i );
15+
if(cnt > 1) answer+= String.valueOf( cnt );
16+
cnt = 1;
17+
}
18+
}
19+
return answer;
20+
}
21+
22+
public static void main(String[] args){
23+
Scanner in=new Scanner( System.in);
24+
sgyj.inflearn.yeji.week2.Solution11 solution11 = new sgyj.inflearn.yeji.week2.Solution11();
25+
System.out.println( solution11.solution( in.nextLine() ) );
26+
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package sgyj.inflearn.study.week2;
2+
3+
import java.util.Scanner;
4+
5+
public class Solution12 {
6+
/*
7+
* Integer.parseInt();
8+
* */
9+
public static String solution(int n , String s){
10+
String answer = "";
11+
for(int i=0; i<n; i++){
12+
String tmp = s.substring( 0,7 ).replace( '#','1' ).replace( '*','0' );
13+
int num = Integer.parseInt( tmp,2 );
14+
answer+=(char)num;
15+
s=s.substring( 7 );
16+
}
17+
return answer;
18+
}
19+
20+
public static void main(String[] args){
21+
Scanner in=new Scanner( System.in);
22+
sgyj.inflearn.yeji.week2.Solution12 solution12 = new sgyj.inflearn.yeji.week2.Solution12();
23+
int n = in.nextInt();
24+
in.nextLine();
25+
String s = in.nextLine();
26+
System.out.println( solution12.solution( n, s ) );
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package sgyj.inflearn.study.week2;
2+
3+
import java.util.ArrayList;
4+
import java.util.Scanner;
5+
6+
public class Solution13 {
7+
public ArrayList<Integer> solution( int n, int[] arr){
8+
ArrayList<Integer> answer = new ArrayList<>();
9+
answer.add( arr[0] );
10+
for(int i=1; i<n; i++){
11+
if(arr[i] > arr[i-1]) answer.add( arr[i] );
12+
}
13+
return answer;
14+
}
15+
16+
public static void main(String[] args){
17+
Solution13 solution13 = new Solution13();
18+
Scanner kb = new Scanner( System.in );
19+
int n = kb.nextInt();
20+
int[] arr = new int[n];
21+
for ( int i = 0; i < n; i++ ) {
22+
arr[i] = kb.nextInt();
23+
}
24+
for(int x : solution13.solution(n,arr)){
25+
System.out.print( x + " " );
26+
}
27+
}
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package sgyj.inflearn.study.week2;
2+
3+
import java.util.Scanner;
4+
5+
public class Solution14 {
6+
7+
public static int solution(int n, int[] arr){
8+
int answer = 1, max=arr[0];
9+
10+
for(int i=1; i<n; i++){
11+
if(arr[i] > max) {
12+
answer++;
13+
max=arr[i];
14+
}
15+
}
16+
17+
return answer;
18+
}
19+
20+
public static void main(String[] args){
21+
Scanner in=new Scanner( System.in);
22+
int input1 = in.nextInt();
23+
int[] ints = new int[input1];
24+
for(int i=0; i<input1; i++){
25+
ints[i] = in.nextInt();
26+
}
27+
System.out.print(solution(input1, ints));
28+
}
29+
30+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package sgyj.inflearn.study.week2;
2+
3+
import java.util.Scanner;
4+
5+
public class Solution15 {
6+
7+
public static String solution(int n, int[] a, int[] b){
8+
String answer = "";
9+
for(int i=0; i<n; i++){
10+
if(a[i] == b[i]){
11+
answer+="D";
12+
}else if(a[i] == 1 && b[i] == 3){
13+
answer+="A";
14+
}else if(a[i] == 2 && b[i] == 1){
15+
answer+="A";
16+
}else if(a[i] == 3 && b[i] == 2){
17+
answer+="A";
18+
}else{
19+
answer+="B";
20+
}
21+
}
22+
23+
return answer;
24+
}
25+
26+
27+
public static void main(String[] args){
28+
Scanner in=new Scanner( System.in);
29+
int input1 = in.nextInt();
30+
in.nextLine();
31+
int[] input2 = new int[input1];
32+
int[] input3 = new int[input1];
33+
for(int i=0; i<input1; i++){
34+
input2[i] = in.nextInt();
35+
}
36+
for(int i=0; i<input1; i++){
37+
input3[i] = in.nextInt();
38+
}
39+
String solution = solution( input1, input2, input3 );
40+
System.out.println(solution);
41+
}
42+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package sgyj.inflearn.study.week2;
2+
3+
import java.util.Scanner;
4+
5+
public class Solution16 {
6+
7+
public int[] solution(int n){
8+
int[] answer = new int[n];
9+
answer[0] = 1;
10+
answer[1] = 1;
11+
12+
for(int i=2; i<n; i++){
13+
answer[i] = answer[i-2] + answer[i-1];
14+
}
15+
16+
return answer;
17+
}
18+
19+
public static void main(String[] args){
20+
Solution16 solution16 = new Solution16();
21+
Scanner kb = new Scanner( System.in );
22+
int n = kb.nextInt();
23+
for(int x : solution16.solution(n)) System.out.print(x + " ");
24+
25+
}
26+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package sgyj.inflearn.study.week2;
2+
3+
import java.util.Scanner;
4+
5+
public class Solution17 {
6+
7+
public int solution(int n){
8+
int answer = 0;
9+
int[] ch = new int[n+1];
10+
for(int i=2; i<=n; i++){
11+
if(ch[i] == 0){
12+
answer++;
13+
for(int j=i; j<=n; j=j+1){
14+
ch[j] = 1;
15+
}
16+
}
17+
}
18+
return answer;
19+
}
20+
21+
public static void main(String[] args){
22+
Solution17 solution17 = new Solution17();
23+
Scanner scanner = new Scanner( System.in );
24+
int n = scanner.nextInt();
25+
System.out.print(solution17.solution(n));
26+
}
27+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package sgyj.inflearn.study.week2;
2+
3+
import java.util.ArrayList;
4+
import java.util.Scanner;
5+
6+
public class Solution18 {
7+
8+
public boolean isPrime(int num){
9+
if(num == 1) return false;
10+
for(int i=2; i<num; i++){
11+
if(num % i ==0) return false;
12+
}
13+
return true;
14+
}
15+
16+
public ArrayList<Integer> solution(int n , int[] arr){
17+
ArrayList<Integer> answer = new ArrayList<>();
18+
19+
for(int i=0; i<n; i++){
20+
int tmp = arr[i];
21+
int res = 0;
22+
while(tmp > 0){
23+
int t = tmp % 10;
24+
res = res * 10 + t;
25+
tmp = tmp/10;
26+
}
27+
if(isPrime(res)) answer.add( res );
28+
}
29+
30+
return answer;
31+
}
32+
33+
public static void main(String[] args){
34+
Solution18 solution18 = new Solution18();
35+
Scanner scanner = new Scanner( System.in );
36+
int n = scanner.nextInt();
37+
int[] arr = new int[n];
38+
for(int i=0; i<n; i++){
39+
arr[i] = scanner.nextInt();
40+
}
41+
for(int x : solution18.solution(n, arr)){
42+
System.out.print(x+" ");
43+
}
44+
}
45+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package sgyj.inflearn.study.week2;
2+
3+
import java.util.Scanner;
4+
5+
public class Solution19 {
6+
7+
public int solution(int n, int[] arr){
8+
int answer = 0, cnt = 0;
9+
for(int i=0; i<n; i++){
10+
if(arr[i] == 1){
11+
cnt++;
12+
answer+=cnt;
13+
}else{
14+
cnt=0;
15+
}
16+
}
17+
18+
return answer;
19+
}
20+
21+
public void main(String[] args){
22+
Solution19 solution19 = new Solution19();
23+
Scanner scanner = new Scanner( System.in );
24+
int n = scanner.nextInt();
25+
26+
int[] arr = new int[n];
27+
28+
for(int i=0; i<n; i++){
29+
arr[i] = scanner.nextInt();
30+
}
31+
32+
System.out.print(solution19.solution(n, arr));
33+
}
34+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package sgyj.inflearn.study.week2;
2+
3+
import java.util.Scanner;
4+
5+
public class Solution20 {
6+
public int[] solution(int n, int[] arr){
7+
int[] answer = new int[n];
8+
9+
for(int i=0; i<n; i++){
10+
int cnt = 1;
11+
for(int j=0; j<n; j++){
12+
if(arr[j] > arr[i]) cnt++;
13+
}
14+
answer[i] = cnt;
15+
}
16+
17+
return answer;
18+
}
19+
20+
public void main(String[] args){
21+
Solution20 solution20 = new Solution20();
22+
Scanner scanner = new Scanner( System.in );
23+
int n = scanner.nextInt();
24+
int[] arr = new int[n];
25+
for(int i=0; i<n; i++){
26+
arr[i] = scanner.nextInt();
27+
}
28+
for(int x : solution20.solution(n, arr)) System.out.print(x + " ");
29+
}
30+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package sgyj.inflearn.yeji.week2;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Scanner;
6+
7+
public class Solution11 {
8+
// KKHSSSSSSSE
9+
// K2HS7E
10+
public String solution(String value){
11+
StringBuilder answer = new StringBuilder();
12+
List<ValueMap> valueMapList = new ArrayList<>();
13+
int target = 0;
14+
int index = 1;
15+
for(int i = 1; i<value.length(); i++){
16+
if(value.charAt( target ) == value.charAt( i )){
17+
index++;
18+
}else{
19+
valueMapList.add( new ValueMap( value.charAt( target ),index ) );
20+
index = 1;
21+
}
22+
target++;
23+
}
24+
valueMapList.add( new ValueMap( value.charAt( target ),index ) );
25+
for(ValueMap map : valueMapList){
26+
answer.append( map.getKey() );
27+
if(map.getValue() != 1){
28+
answer.append( map.getValue() );
29+
}
30+
31+
}
32+
return answer.toString();
33+
}
34+
35+
public static void main(String[] args){
36+
Scanner in=new Scanner( System.in);
37+
Solution11 solution11 = new Solution11();
38+
System.out.println( solution11.solution( in.nextLine() ) );
39+
40+
}
41+
42+
class ValueMap{
43+
private Character key;
44+
private Integer value;
45+
46+
ValueMap(char key, int value){
47+
this.key = key;
48+
this.value = value;
49+
}
50+
51+
public char getKey () {
52+
return key;
53+
}
54+
55+
public int getValue(){
56+
return value;
57+
}
58+
}
59+
60+
}

0 commit comments

Comments
 (0)