Skip to content

인프런 3주차 #7

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

Merged
merged 12 commits into from
Jan 17, 2023
Merged
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
44 changes: 44 additions & 0 deletions src/main/java/sgyj/inflearn/seunggu/week3/Solution29.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package sgyj.inflearn.seunggu.week3;

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

public class Solution29 {
/**
* @title : 연속된 자연수의 합
* @description : N입력으로 양의 정수 N이 입력되면 2개 이상의 연속된 자연수의 합으로 정수 N을 표현하는 방법의 가짓수를 출력하는 프로그램을 작성하세요.
* 만약 N=15이면
* 7+8=15
* 4+5+6=15
* 1+2+3+4+5=15
* 와 같이 총 3가지의 경우가 존재한다.
* @input : 첫 번째 줄에 양의 정수 N(7<=N<1000)이 주어집니다.
* @output : 첫 줄에 총 경우수를 출력합니다.
*/
public static void main ( String[] args ) {
try ( BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) ) ) {
System.out.println(solution( reader ));
} catch ( IOException e ) {
e.printStackTrace();
}
}

static int solution ( BufferedReader reader ) throws IOException {
int number = Integer.parseInt( reader.readLine() );
int result = 0;
for(int i=1; i<number; i++) {
int sum = i;
for(int j=i+1; j <number; j++) {
sum += j;
if(sum > number) {
break;
}
if(sum == number) {
result++;
}
}
}
return result;
}
}
66 changes: 66 additions & 0 deletions src/main/java/sgyj/inflearn/seunggu/week3/Solution30.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package sgyj.inflearn.seunggu.week3;

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

public class Solution30 {
/**
* @title : 최대 길이 연속부분수열
* @description : 0과 1로 구성된 길이가 N인 수열이 주어집니다. 여러분은 이 수열에서 최대 k번을 0을 1로 변경할 수 있습니다. 여러분이 최대 k번의 변경을 통해 이 수열에서 1로만 구성된 최대 길이의 연속부분수열을 찾는 프로그램을 작성하세요.
* 만약 길이가 길이가 14인 다음과 같은 수열이 주어지고 k=2라면
* 1 1 0 0 1 1 0 1 1 0 1 1 0 1
* 여러분이 만들 수 있는 1이 연속된 연속부분수열은
* 1 1 1 1 1 1 1 1
* 이며 그 길이는 8입니다.
* @input : 첫 번째 줄에 수열의 길이인 자연수 N(5<=N<100,000)이 주어집니다.
* 두 번째 줄에 N길이의 0과 1로 구성된 수열이 주어집니다.
* @output : 첫 줄에 최대 길이를 출력하세요.
*/
public static void main ( String[] args ) {
try ( BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) ) ) {
System.out.println(solution( reader ));
} catch ( IOException e ) {
e.printStackTrace();
}
}

static int solution ( BufferedReader reader ) throws IOException {
String[] n = reader.readLine().split( " " );
int length = Integer.parseInt( n[0] );
int k = Integer.parseInt( n[1] );
int result = 0;
int[] array = Arrays.stream( reader.readLine().split( " " ) ).mapToInt( Integer::parseInt ).toArray();
int[] zeroIndexArray = getZeroIndexArray( array );
int startIndex = 0;
int zeroCount = 0;
int sumLength = 0;
int zeroIndex = 0;
while(startIndex < length - k) {
for(int i=startIndex; i< length; i++) {
if( array[i] == 0 ) {
if(zeroCount == k) {
break;
}
zeroCount++;
}
sumLength++;
}
result = Math.max( result, sumLength );
sumLength=0;
zeroCount = 0;
startIndex = zeroIndexArray[zeroIndex++] + 1;
}

return result;
}

private static int[] getZeroIndexArray ( int[] array ) {
StringBuilder stringBuilder = new StringBuilder();
for ( int i = 0; i < array.length; i++ ) {
if( array[i] == 0) stringBuilder.append( i ).append( " " );
}
return Arrays.stream( stringBuilder.toString().split( " " ) ).mapToInt( Integer::parseInt ).toArray();
}
}
41 changes: 41 additions & 0 deletions src/main/java/sgyj/inflearn/study/week3/Solution21.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package sgyj.inflearn.study.week3;

import java.util.Scanner;

public class Solution21 {

public static void main ( String[] args ) {
Solution21 solution21 = new Solution21();
Scanner scanner = new Scanner( System.in );
int n = scanner.nextInt();
int[][] arr = new int[n][n];
for ( int i = 0; i < arr.length; i++ ) {
for ( int j = 0; j < arr.length; j++ ) {
arr[i][j] = scanner.nextInt();
}
}
System.out.println(solution21.solution(n,arr));
}

private int solution ( int n, int[][] arr ) {
int answer = Integer.MIN_VALUE;
int sum1, sum2;
for(int i = 0; i<n; i++){
sum1 = sum2 = 0;
for(int j = 0; j<n; j++){
sum1 += arr[i][j];
sum2 += arr[j][i];
}
answer=Math.max( answer, sum1);
answer=Math.max( answer, sum2 );
}
sum1=sum2=0;
for(int i=0; i<n; i++){
sum1+=arr[i][i];
sum2+=arr[i][n-i-1];
}
answer=Math.max( answer, sum1 );
answer=Math.max( answer,sum2 );
return answer;
}
}
40 changes: 40 additions & 0 deletions src/main/java/sgyj/inflearn/study/week3/Solution22.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package sgyj.inflearn.study.week3;

import java.util.Scanner;

public class Solution22 {

static int[] dx = {-1,0,1,0};
static int[] dy = {0,1,0,-1};
public static int solution ( int n, int[][] arr ){
int answer = 0;
for ( int i = 0; i < arr.length; i++ ) {
for ( int j = 0; j < arr.length; j++ ) {
boolean flag = true;
for(int k=0; k<4; k++){
int nx = i+dx[k];
int ny = j+dy[k];
if(nx>=0 && nx<n && ny>=0 && ny<n && arr[nx][ny]>=arr[i][j]){
flag=false;
break;
}
}
if(flag) answer++;
}
}
return answer;
}

public static void main ( String[] args ) {
Solution21 solution21 = new Solution21();
Scanner scanner = new Scanner( System.in );
int n = scanner.nextInt();
int[][] arr = new int[n][n];
for ( int i = 0; i < arr.length; i++ ) {
for ( int j = 0; j < arr.length; j++ ) {
arr[i][j] = scanner.nextInt();
}
}
System.out.println(solution(n,arr));
}
}
39 changes: 39 additions & 0 deletions src/main/java/sgyj/inflearn/study/week3/Solution23.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package sgyj.inflearn.study.week3;

import java.util.Scanner;

public class Solution23 {

public static int solution ( int n, int[][] arr ){
int answer = 0, max=Integer.MIN_VALUE;
for(int i = 1; i<=n; i++){
int cnt = 0;
for(int j=1; j<=n; j++){
for(int k=1; k<=5; k++){
if(arr[i][k] == arr[j][k]){
cnt++;
break;
}
}
}
if(cnt>max){
max=cnt;
answer=i;
}
}
return answer;
}

public static void main(String[] args){
Scanner scanner = new Scanner( System.in );
int n = scanner.nextInt();
int[][] input = new int[n+1][6];
for(int i=0; i<n; i++){
for(int j=1; j<=5; j++){
input[i][j] = scanner.nextInt();
}
}
System.out.println(solution( n, input ));
}

}
44 changes: 44 additions & 0 deletions src/main/java/sgyj/inflearn/study/week3/Solution24.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package sgyj.inflearn.study.week3;

import java.util.Scanner;

public class Solution24 {

public static int solution ( int n, int m, int[][] arr ){
int answer = 0;
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int cnt = 0;
for(int k=0; k<m; k++){
int pi=0,pj=0;
for(int s=0; s<n; s++){
if(arr[k][s]==i) pi=s;
if(arr[k][s]==j) pj=s;
}
if(pi<pj) cnt++;
}
if(cnt == m){
answer++;
}
}
}
return answer;
}


public static void main(String[] args){
Scanner sc = new Scanner( System.in );
String[] input = sc.nextLine().split( " " );
int n = Integer.parseInt(input[0]);
int m = Integer.parseInt( input[1] );
int[][] inputValue = new int[m][n];

for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
inputValue[i][j] = sc.nextInt();
}
}
System.out.println(solution(n,m,inputValue));
}

}
37 changes: 37 additions & 0 deletions src/main/java/sgyj/inflearn/study/week3/Solution25.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package sgyj.inflearn.study.week3;

import java.util.ArrayList;
import java.util.Scanner;

public class Solution25 {

public static ArrayList<Integer> solution(int n,int m, int[] a, int[] b){
ArrayList<Integer> answer = new ArrayList<>();
int p1=0,p2=0;

while ( p1<n && p2<m ){
if(a[p1]<b[p2]) answer.add( a[p1++] );
else answer.add(b[p2++]);
}
while ( p1<n ) answer.add( a[p1++] );
while ( p2<m ) answer.add( b[p2++] );
return answer;
}

public static void main ( String[] args ) {
Scanner scanner = new Scanner( System.in );
int n = scanner.nextInt();
int[] a = new int[n];
for(int i=0; i<n; i++){
a[i] = scanner.nextInt();
}
int m = scanner.nextInt();
int[] b = new int[m];
for(int i=0; i<m; i++){
b[i] = scanner.nextInt();
}
for(int x : solution(n,m,a,b)) System.out.print(x + " ");

}

}
46 changes: 46 additions & 0 deletions src/main/java/sgyj/inflearn/yeji/week3/Solution21.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package sgyj.inflearn.yeji.week3;

import java.util.Scanner;

public class Solution21 {

public static int solution(int n, int[][] input){
int answer = 0;

for(int i=0; i<n; i++){
int index = 0;
int garo = 0;
int sero = 0;
while (index < n){
garo += input[i][index];
sero += input[index][i];
index++;
}
if(garo > sero && answer < garo) answer = garo;
else if(garo < sero && answer < sero) answer = sero;
}
int compare1 = 0;
int compare2 = 0;
for(int i=0; i<n; i++){
compare1 += input[i][i];
compare2 += input[i][n-1-i];
}
if(compare1 > answer) answer = compare1;
if(compare2 > answer) answer = compare2;
return answer;

}

public static void main(String[] args){
Scanner scanner = new Scanner( System.in );
int n = scanner.nextInt();
int[][] input = new int[n][n];
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
input[i][j] = scanner.nextInt();
}
}
System.out.println(solution( n,input ));

}
}
Loading