Skip to content

Commit 5424026

Browse files
Merge pull request #6 from algorithm-cote-study/seunggu/week3
feat: 3주차 나머지 문제
2 parents 45932d7 + 3775a6b commit 5424026

File tree

14 files changed

+279
-0
lines changed

14 files changed

+279
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package sgyj.inflearn.seunggu.week3;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
7+
public class Solution29 {
8+
/**
9+
* @title : 연속된 자연수의 합
10+
* @description : N입력으로 양의 정수 N이 입력되면 2개 이상의 연속된 자연수의 합으로 정수 N을 표현하는 방법의 가짓수를 출력하는 프로그램을 작성하세요.
11+
* 만약 N=15이면
12+
* 7+8=15
13+
* 4+5+6=15
14+
* 1+2+3+4+5=15
15+
* 와 같이 총 3가지의 경우가 존재한다.
16+
* @input : 첫 번째 줄에 양의 정수 N(7<=N<1000)이 주어집니다.
17+
* @output : 첫 줄에 총 경우수를 출력합니다.
18+
*/
19+
public static void main ( String[] args ) {
20+
try ( BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) ) ) {
21+
System.out.println(solution( reader ));
22+
} catch ( IOException e ) {
23+
e.printStackTrace();
24+
}
25+
}
26+
27+
static int solution ( BufferedReader reader ) throws IOException {
28+
int number = Integer.parseInt( reader.readLine() );
29+
int result = 0;
30+
for(int i=1; i<number; i++) {
31+
int sum = i;
32+
for(int j=i+1; j <number; j++) {
33+
sum += j;
34+
if(sum > number) {
35+
break;
36+
}
37+
if(sum == number) {
38+
result++;
39+
}
40+
}
41+
}
42+
return result;
43+
}
44+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package sgyj.inflearn.seunggu.week3;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.Arrays;
7+
8+
public class Solution30 {
9+
/**
10+
* @title : 최대 길이 연속부분수열
11+
* @description : 0과 1로 구성된 길이가 N인 수열이 주어집니다. 여러분은 이 수열에서 최대 k번을 0을 1로 변경할 수 있습니다. 여러분이 최대 k번의 변경을 통해 이 수열에서 1로만 구성된 최대 길이의 연속부분수열을 찾는 프로그램을 작성하세요.
12+
* 만약 길이가 길이가 14인 다음과 같은 수열이 주어지고 k=2라면
13+
* 1 1 0 0 1 1 0 1 1 0 1 1 0 1
14+
* 여러분이 만들 수 있는 1이 연속된 연속부분수열은
15+
* 1 1 1 1 1 1 1 1
16+
* 이며 그 길이는 8입니다.
17+
* @input : 첫 번째 줄에 수열의 길이인 자연수 N(5<=N<100,000)이 주어집니다.
18+
* 두 번째 줄에 N길이의 0과 1로 구성된 수열이 주어집니다.
19+
* @output : 첫 줄에 최대 길이를 출력하세요.
20+
*/
21+
public static void main ( String[] args ) {
22+
try ( BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) ) ) {
23+
System.out.println(solution( reader ));
24+
} catch ( IOException e ) {
25+
e.printStackTrace();
26+
}
27+
}
28+
29+
static int solution ( BufferedReader reader ) throws IOException {
30+
String[] n = reader.readLine().split( " " );
31+
int length = Integer.parseInt( n[0] );
32+
int k = Integer.parseInt( n[1] );
33+
int result = 0;
34+
int[] array = Arrays.stream( reader.readLine().split( " " ) ).mapToInt( Integer::parseInt ).toArray();
35+
int[] zeroIndexArray = getZeroIndexArray( array );
36+
int startIndex = 0;
37+
int zeroCount = 0;
38+
int sumLength = 0;
39+
int zeroIndex = 0;
40+
while(startIndex < length - k) {
41+
for(int i=startIndex; i< length; i++) {
42+
if( array[i] == 0 ) {
43+
if(zeroCount == k) {
44+
break;
45+
}
46+
zeroCount++;
47+
}
48+
sumLength++;
49+
}
50+
result = Math.max( result, sumLength );
51+
sumLength=0;
52+
zeroCount = 0;
53+
startIndex = zeroIndexArray[zeroIndex++] + 1;
54+
}
55+
56+
return result;
57+
}
58+
59+
private static int[] getZeroIndexArray ( int[] array ) {
60+
StringBuilder stringBuilder = new StringBuilder();
61+
for ( int i = 0; i < array.length; i++ ) {
62+
if( array[i] == 0) stringBuilder.append( i ).append( " " );
63+
}
64+
return Arrays.stream( stringBuilder.toString().split( " " ) ).mapToInt( Integer::parseInt ).toArray();
65+
}
66+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package sgyj.inflearn.seunggu.week3;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.BufferedReader;
6+
import org.junit.jupiter.api.DisplayName;
7+
import org.junit.jupiter.api.Test;
8+
import sgyj.inflearn.common.TestFileUtil;
9+
10+
@DisplayName( "연속된 자연수의 합" )
11+
class Solution29Test {
12+
@Test
13+
@DisplayName( "연속된 자연수의 합 테스트 케이스 1" )
14+
void test_case_1 () throws Exception {
15+
// given
16+
BufferedReader reader = TestFileUtil.getReader( this.getClass(), "static/solution29/test_case1.txt" );
17+
// when
18+
int solution = Solution29.solution( reader );
19+
20+
// then
21+
assertEquals( 1, solution );
22+
23+
}
24+
25+
@Test
26+
@DisplayName( "연속된 자연수의 합 테스트 케이스 2" )
27+
void test_case_2 () throws Exception {
28+
// given
29+
BufferedReader reader = TestFileUtil.getReader( this.getClass(), "static/solution29/test_case2.txt" );
30+
// when
31+
int solution = Solution29.solution( reader );
32+
33+
// then
34+
assertEquals( 1, solution );
35+
36+
}
37+
38+
@Test
39+
@DisplayName( "연속된 자연수의 합 테스트 케이스 3" )
40+
void test_case_3 () throws Exception {
41+
// given
42+
BufferedReader reader = TestFileUtil.getReader( this.getClass(), "static/solution29/test_case3.txt" );
43+
// when
44+
int solution = Solution29.solution( reader );
45+
46+
// then
47+
assertEquals( 3, solution );
48+
49+
}
50+
51+
@Test
52+
@DisplayName( "연속된 자연수의 합 테스트 케이스 4" )
53+
void test_case_4 () throws Exception {
54+
// given
55+
BufferedReader reader = TestFileUtil.getReader( this.getClass(), "static/solution29/test_case4.txt" );
56+
// when
57+
int solution = Solution29.solution( reader );
58+
59+
// then
60+
assertEquals( 5, solution );
61+
62+
}
63+
64+
@Test
65+
@DisplayName( "연속된 자연수의 합 테스트 케이스 5" )
66+
void test_case_5 () throws Exception {
67+
// given
68+
BufferedReader reader = TestFileUtil.getReader( this.getClass(), "static/solution29/test_case5.txt" );
69+
// when
70+
int solution = Solution29.solution( reader );
71+
72+
// then
73+
assertEquals( 8, solution );
74+
75+
}
76+
77+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package sgyj.inflearn.seunggu.week3;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.io.BufferedReader;
6+
import org.junit.jupiter.api.DisplayName;
7+
import org.junit.jupiter.api.Test;
8+
import sgyj.inflearn.common.TestFileUtil;
9+
10+
@DisplayName( "최대 길이 연속부분수열" )
11+
class Solution30Test {
12+
@Test
13+
@DisplayName( "최대 길이 연속부분수열 테스트 케이스 1" )
14+
void test_case_1 () throws Exception {
15+
// given
16+
BufferedReader reader = TestFileUtil.getReader( this.getClass(), "static/solution30/test_case1.txt" );
17+
// when
18+
int solution = Solution30.solution( reader );
19+
20+
// then
21+
assertEquals( 8, solution );
22+
23+
}
24+
25+
@Test
26+
@DisplayName( "최대 길이 연속부분수열 테스트 케이스 2" )
27+
void test_case_2 () throws Exception {
28+
// given
29+
BufferedReader reader = TestFileUtil.getReader( this.getClass(), "static/solution30/test_case2.txt" );
30+
// when
31+
int solution = Solution30.solution( reader );
32+
33+
// then
34+
assertEquals( 13, solution );
35+
36+
}
37+
38+
@Test
39+
@DisplayName( "최대 길이 연속부분수열 테스트 케이스 3" )
40+
void test_case_3 () throws Exception {
41+
// given
42+
BufferedReader reader = TestFileUtil.getReader( this.getClass(), "static/solution30/test_case3.txt" );
43+
// when
44+
int solution = Solution30.solution( reader );
45+
46+
// then
47+
assertEquals( 15, solution );
48+
49+
}
50+
51+
@Test
52+
@DisplayName( "최대 길이 연속부분수열 테스트 케이스 4" )
53+
void test_case_4 () throws Exception {
54+
// given
55+
BufferedReader reader = TestFileUtil.getReader( this.getClass(), "static/solution30/test_case4.txt" );
56+
// when
57+
int solution = Solution30.solution( reader );
58+
59+
// then
60+
assertEquals( 36, solution );
61+
62+
}
63+
64+
@Test
65+
@DisplayName( "최대 길이 연속부분수열 테스트 케이스 5" )
66+
void test_case_5 () throws Exception {
67+
// given
68+
BufferedReader reader = TestFileUtil.getReader( this.getClass(), "static/solution30/test_case5.txt" );
69+
// when
70+
int solution = Solution30.solution( reader );
71+
72+
// then
73+
assertEquals( 50, solution );
74+
75+
}
76+
77+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
12
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
19
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
120
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
720
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
450
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
14 2
2+
1 1 0 0 1 1 0 1 1 0 1 1 0 1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
20 2
2+
1 0 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 1 0 1
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
100 3
2+
1 0 0 0 0 1 0 0 0 1 0 1 1 0 0 0 0 0 1 1 0 1 0 0 0 1 0 1 0 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 0 1 1 0 1 1 1 0 1 1 1 0 0 0 1 1 1 1 0 1 1 0 0 1 0 1 1 0 1 1 0 0 1 0 0 1 1 1 1 1 1 1 1 0 1 0 1 0 1 1 0 1 0 0 1

src/test/resources/static/solution30/test_case4.txt

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.

src/test/resources/static/solution30/test_case5.txt

Lines changed: 2 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)