Algorithm Study

[Baekjoon] 2week

4leaf3 2023. 10. 28. 19:32

2주차 문제

 

1) 10798 - 세로 읽기(발표자 이슬아)

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        // 5줄 총 15개 2차원 배열 선언
        char[][] str = new char[5][15];

        // 입력받은 문자들 그대로 출력해주기
        for (int i = 0; i < 5; i++) {
            String input = scan.next();
            // 문자열 길이만큼 문자 넣기
            for (int j = 0; j < input.length(); j++) {
                str[i][j] = input.charAt(j);
            }
        }

        // 세로로 출력
        for (int i = 0; i < 15; i++) {
            for (int j = 0; j < 5; j++) {
                if (str[j][i] == '\0' || str[j][i] == '\u0000') // 빈 문자열이면 출력하지 않기
                    continue;
                System.out.print(str[j][i]);
            }
        }
    }
}

추가 코드(차민재)

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


public class Main {
   
   public static void main(String[] args) throws IOException {
      
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      
      String a = br.readLine();
      String b = br.readLine();
      String c = br.readLine();
      String d = br.readLine();
      String e = br.readLine();
      
      for (int i = 0; i < 15; i++) {
         if (i < a.length())
            System.out.print(a.charAt(i));
         if (i < b.length())
            System.out.print(b.charAt(i));
         if (i < c.length())
            System.out.print(c.charAt(i));
         if (i < d.length())
            System.out.print(d.charAt(i));
         if (i < e.length())
            System.out.print(e.charAt(i));
      }
      
      br.close();
   }
}

 

2) 2839 - 설탕 배달(발표자 박나래)

import java.util.Scanner;

public class Main {
   
   public static void main(String[] args) {
      
      Scanner scan = new Scanner(System.in);
      
      int N = scan.nextInt();
      int count = 0;
      
      while (N >= 3) {
         if (N % 5 != 0) {
            N -= 3;
            count++;
            
            
            if ( N < 5 && N % 3 != 0) {
               count = -1;
               break;
            }
            
         } else {
            
            //5로 나눈 나머지가 0일 경우 > 5의 배수
            count += N / 5;
            break;
         }
         
      }
      
      System.out.println(count);

      // 값이 5로 나누어 떨어지면 > 몫이 봉지 개수
      // 값이 5로 나누어지지 않으면 > 5로 나눈 몫을 카운트에 넣고, 나머지를 3으로 나누어 떨어지는지 보고 아니면 -3 하고 도 안되면 -1 반환?
      
      
   }
}

 

3) 2869 - 달팽이는 올라가고 싶다(발표자 염현빈)

import java.util.Scanner;

//백준 2869
public class Q2869 {
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String input = scan.nextLine();

        String[] math = input.split(" ");
        int a = Integer.parseInt(math[0]);      //올라가는 거리
        int b = Integer.parseInt(math[1]);      //내려오는 거리
        int v = Integer.parseInt(math[2]);      //정상까지의 높이

        //a-b //하루에 올라가는 거리
        int day = (v-a)/(a-b) ;

        if((v-a) % (a-b) != 0){
            day = day + 1;
        }
        System.out.print(day+1);
    }
}

추가 코드(김수진)

int result=0;
		
		int d=c-a;
		
		if(d%(a-b) > 0) {
			result+=(d/(a-b))+1+1;
		}else if(d%(a-b) == 0) {
			result+=(d/(a-b))+1;
			
		}
		
		
		
		System.out.println(result);

 

4) 11170 - 0의 개수(발표자 오승현)

import java.util.Scanner;

public class Q014 {

    public static void main(String[] args) {

                Scanner sc = new Scanner(System.in);
                //몇개 입력할지
                int T = sc.nextInt();

                for (int i = 0; i < T; i++) {
                    int N = sc.nextInt();
                    int M = sc.nextInt();

                    int num = 0;


                    for ( int a = N; a <= M; a++) {

                        //따로
                        int b=a;

                        if (b == 0) {
                            num++;
                            continue;
                        }

                        while (b != 0) {

                            if (b % 10 == 0) {
                                num++;
                            }
                            // 1의 자리 10의 자리 100의 자리~
                            b /= 10;
                        }

                    }

                    System.out.println(num);

                }
                sc.close();
    }
}

추가 코드(차민재)

  public static long countChar(String temp, char ch) {
      return temp.chars().filter(c -> c == ch).count();
   }

 

 

2주차 마무리

2주차 인증샷