코드업 기초 100제 (자바): 1078-1099

8 minute read

모든 문제는 코드업에서 참고했습니다.

기초-종합

1078 : 짝수 합 구하기

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();

        // Sn = n(n+1) = sum of even numbers = 2+4+6 +...+2n
        // input = 2n
        int n = input/2;
        int evenSum = n * (n+1);
        System.out.println(evenSum);
        scanner.close();
    }
}

or

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();

        int evenSum = 0;
        for (int i = 2; i <= input; i += 2) {
            evenSum += i;
        }
        System.out.println(evenSum);
        scanner.close();
    }
}

1079 : 원하는 문자가 입력될 때까지 반복 출력

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String[] inputArray = scanner.nextLine().split(" ");
        String c = "q";

        for (String s: inputArray) {
            System.out.println(s);

            if (s.equals(c)) {
                break;
            }
        }
        scanner.close();
    }
}

1080 : 원언제까지 더해야 할까?

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();
        int sum = 0;
        for (int i = 0; i <= 45; i++) {  // S(44)=990 < 1000 < 1035 = S(45)
            sum += i;
            if (sum >= input) {
                System.out.println(i);
                break;
            }
        }
        scanner.close();
    }
}

or

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();
        // goal: find min n s.t. n(n+1)/2 >= input     (sum = n(n+1)/2)
        // n^2 + n >= 2*input
        // quadratic formula : a=b=1, c = -2 *input
        double discriminant = 1 + 8.0 * input; // b^2 - 4ac
        double r1 = (-1 + Math.pow(discriminant, 0.5)) / 2.0; // (-b + Math.pow(result, 0.5)) / (2.0 * a);
        System.out.println((int)Math.ceil(r1));
        scanner.close();
    }
}

1081 : 주사위를 2개 던지면?

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int firstMax = scanner.nextInt();
        int secondMax = scanner.nextInt();

        for (int i = 1; i <= firstMax; i++) {
            for(int j = 1; j <= secondMax; j++) {
                System.out.println(i + " " + j);
            }
        }
        scanner.close();
    }
}

1082 : 16진수 구구단?

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int radix = 16;
        int input = scanner.nextInt(radix);

        for (int i = 1; i < radix; i++) {
            System.out.printf("%X*%X=%X%n", input, i, input*i);
        }
        scanner.close();
    }
}

1083 : 3 6 9 게임의 왕이 되자!

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();

        for (int i = 1; i <= input; i++) {
            if (i%3==0) {
                System.out.print("X ");
            } else {
                System.out.print(i + " ");
            }
        }
        scanner.close();
    }
}

1084 : 빛 섞어 색 만들기

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int r = scanner.nextInt();
        int g = scanner.nextInt();
        int b = scanner.nextInt();

        for (int i = 0; i < r; i++) {
            for (int j = 0; j < g; j++) {
                for (int k = 0; k < b; k++) {
                    System.out.printf("%d %d %d %n", i, j, k);
                }
            }
        }
        System.out.println(r*g*b);
        scanner.close();
    }
}

1085 : 소리 파일 저장용량 계산

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int h = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();
        int s = scanner.nextInt();

        double conversionRatio = 8 * 1024*1024; // bit -> mb
        double sizeInMB = 1/conversionRatio;
        sizeInMB *= h * b * c * s;
        System.out.printf("%.1f MB", sizeInMB);
        scanner.close();
    }
}

1086 : 그림 파일 저장용량 계산

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int w = scanner.nextInt();
        int h = scanner.nextInt();
        int b = scanner.nextInt();

        double sizeInMB = (w * h * b)/(8.0 * 1024*1024);
        System.out.printf("%.2f MB", sizeInMB);
        scanner.close();
    }
}

1087 : 여기까지! 이제 그만~

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();
        // goal: find min n s.t. n(n+1)/2 >= input     (sum = n(n+1)/2)
        // n^2 + n >= 2*input
        // quadratic formula : a=b=1, c = -2 *input
        double discriminant = 1 + 8.0 * input; // b^2 - 4ac
        double r1 = (-1 + Math.pow(discriminant, 0.5)) / 2.0; // (-b + Math.pow(result, 0.5)) / (2.0 * a);
        int num = (int)Math.ceil(r1);
        System.out.println(num*(num+1)/2);
        scanner.close();
    }
}

or

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();
        int sum = 0;

        for (int i = 0; i <= 14142; i++) {  // S(14141)=99991011 < 100,000,000 < 100005153 = S(14142)
            sum += i;
            if (sum >= input) {
                System.out.println(sum);
                break;
            }
        }
        scanner.close();
    }
}

1088 : 3의 배수는 통과?

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int input = scanner.nextInt();
        for (int i = 1; i <= input; i++) {
            if (i%3 == 0) continue;
            System.out.print(i + " ");
        }
        scanner.close();
    }
}

1089 : 수 나열1

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int d = scanner.nextInt();
        int n = scanner.nextInt();
        // a_n = a_1+ (n-1)d
        System.out.println(a + (n-1)*d);
        scanner.close();
    }
}

1090 : 수 나열2

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int r = scanner.nextInt();
        int n = scanner.nextInt();

        // a_n = a_1 * (r^(n-1))
        long result = a * (long)Math.pow(r, n-1);
        System.out.println(result);
        scanner.close();
    }
}

1091 : 수 나열3

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int r = scanner.nextInt();
        int d = scanner.nextInt();
        int n = scanner.nextInt();

        for (int i =1; i < n; i++) {
            a =  a * r +1;
        }
        System.out.println(a);
        scanner.close();
    }
}

1092 : 함께 문제 푸는 날

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        int c = scanner.nextInt();

        int lcm = lcm(lcm(a,b), c); // lcm(a,b,c) = lcm(lcm(a,b), c);

        System.out.println(lcm);
        scanner.close();
    }
}

기초-1차원배열

1093 : 이상한 출석 번호 부르기1

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count = Integer.parseInt(scanner.nextLine());
        int n = 23;
        int[] countArray = new int[n];

        for (int i = 0; i < count; i++) {
            int called = scanner.nextInt();
            countArray[called-1]++;
        }

        for (int i = 0; i < n; i++) {
            System.out.printf("%d ", countArray[i]);
        }
        scanner.close();
    }
}

1094 : 이상한 출석 번호 부르기2

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count = Integer.parseInt(scanner.nextLine());
        int[] countArray = new int[count];

        for (int i = 0; i < count; i++) {
            int called = scanner.nextInt();
            countArray[i] = called;
        }

        for (int i = count-1; i >= 0; i--) {
            System.out.printf("%d ", countArray[i]);
        }
        scanner.close();
    }
}

1095 : 이상한 출석 번호 부르기3

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int count = Integer.parseInt(scanner.nextLine());
        int min = 23; //Integer.MAX_VALUE;

        for (int i = 0; i < count; i++) {
            int called = scanner.nextInt();
            if (called < min) {
                min = called;
            }
        }
        System.out.println(min);
        scanner.close();
    }
}

기초-2차원배열

1096 : 바둑판에 흰 돌 놓기

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[][] goboard = new int[19][19];
        int count = Integer.parseInt(scanner.nextLine());

        for (int i = 0; i < count; i++) {
            int x = scanner.nextInt();
            int y = scanner.nextInt();
            goboard[x-1][y-1] = 1;
            scanner.nextLine();
        }

        for (int i = 0; i < goboard.length; i++) {
            for (int j = 0; j < goboard[0].length; j++) {
                System.out.printf("%d ", goboard[i][j]);
            }
            System.out.println();
        }
        scanner.close();
    }
}

1097 : 바둑알 십자 뒤집기

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int length = 19;
        int[][] goboard = new int[length][length];

        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {
                goboard[i][j] = scanner.nextInt();
            }
            scanner.nextLine();
        }

        int count = Integer.parseInt(scanner.nextLine());
        for (int i = 0; i < count; i++) {
            int x = scanner.nextInt();
            int y = scanner.nextInt();

            for (int j = 0; j < length; j++) {
                goboard[x-1][j] = goboard[x-1][j] == 0 ? 1 : 0;
            }
            for (int j = 0; j < length; j++) {
                goboard[j][y-1] = goboard[j][y-1] == 0 ? 1 : 0;
            }
            scanner.nextLine();
        }
        scanner.close();

        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length; j++) {
                System.out.printf("%d ", goboard[i][j]);
            }
            System.out.println();
        }
    }
}

1098 : 설탕과자 뽑기

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int h = scanner.nextInt();
        int w = scanner.nextInt();
        scanner.nextLine();
        int n = Integer.parseInt(scanner.nextLine());
        int[][] board = new int[w][h];

        for (int i = 0; i < n; i++) {
            String[] barInfo = scanner.nextLine().split(" ");
            int l = Integer.parseInt(barInfo[0]);
            int d = Integer.parseInt(barInfo[1]); // horizontal 0, vertical 1
            int x = Integer.parseInt(barInfo[2])-1;
            int y = Integer.parseInt(barInfo[3])-1;

            if (d == 0) {
                //hori
                for (int j = y; j < y+l; j++) {
                    if (j >= w) break;
                    board[x][j] = 1;
                }
            } else {
                //d == 1
                for (int j = x; j < x+l; j++) {
                    if (j >= h) break;
                    board[j][y] = 1;
                }
            }
        }

        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                System.out.printf("%d ", board[i][j]);
            }
            System.out.println();
        }
        scanner.close();
    }
}

1099 : 성실한 개미

import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = 10;
        int[][] board = new int[n][n];
        int cx = 1;
        int cy = 1;

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                board[i][j] = scanner.nextInt();
            }
            scanner.nextLine();
        }

        while(true){
            if (board[cx][cy] == 2) {
                // found the food
                board[cx][cy] = 9;
                break;
            }
            board[cx][cy] = 9;

            if (cy != n-1 && board[cx][cy+1] != 1) {
                // move to the rt
                cy += 1;
            } else if (cx != n-1 && board[cx+1][cy] != 1) {
                cx +=1;
            } else {
                break;
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.printf("%d ", board[i][j]);
            }
            System.out.println();
        }
        scanner.close();
    }
}

Leave a comment