[CodeUp] C언어 기초 100제 (1081~1090) - Java

황은하·2021년 5월 28일
0

알고리즘

목록 보기
42/100
post-thumbnail

✔ 1081

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= m; j++) {
                System.out.println(i + " " + j);
            }
        }
    }
}

✔ 1082

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String n = sc.nextLine();
        int decimal = Integer.parseInt(n, 16);
        for (int i = 1; i < 16; i++) {
            System.out.println(n + "*" + Integer.toHexString(i).toUpperCase() + "=" + Integer.toHexString(decimal * i).toUpperCase());

        }
    }
}

✔ 1083

import java.util.Scanner;

public class Main{
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            if (i == 3 || i == 6 || i == 9) {
                System.out.print("X ");
            } else {
                System.out.print(i+" ");
            }
        }
    }
}

✔ 1084

import java.util.Scanner;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException{
        Scanner sc = new Scanner(System.in);
        int r = sc.nextInt();
        int g = sc.nextInt();
        int b = sc.nextInt();

        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        int count = 0;

        for (int i = 0; i < r; i++) {
            for (int j = 0; j < g; j++) {
                for (int k = 0; k < b; k++) {
                    bw.write(i + " " + j + " " + k + "\n");
                    count++;
                }
            }
        }
        bw.write(String.valueOf(count));
        bw.flush();
    }
}

✔ 1085

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long h = sc.nextLong();  // Hz
        long b = sc.nextLong();  // 비트 수
        long c = sc.nextLong();  // 채널 - 모노, 스테레오
        long s = sc.nextLong();  // 녹음 시간
        double total = h * b * c * s;

        total /= Math.pow(2, 23);
        System.out.println(String.format("%.1f MB", total));
    }
}

소수점 두 번째 자리에서 반올림 한 MB 단위로 출력한다.

  • 8 (2^3) bit = 1Byte
  • 1024 (2^10) Byte = 1KB
  • 1024 (2^10) KB = 1MB

2^23을 나눠준 뒤 String.format을 사용하여 한 자리까지 출력시킨다.


✔ 1086

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int w = sc.nextInt();  // 가로 해상도, 정수, 1_1024
        int h = sc.nextInt();  // 세로 해상도, 정수, 1_1024
        int b = sc.nextInt();  // 한 픽셀을 저장하기 위한 비트, 40 이하의 4의 배수
        double total = w * h * b;

        total /= Math.pow(2, 23);
        System.out.println(String.format("%.2f MB", total));
    }
}

가로, 세로, 비트 수를 다 곱해서 소수점 두 자리 까지 MB단위로 출력한다.


✔ 1087

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int sum = 0, i = 1;
        while (sum < n) {
            sum += i++;
        }
        System.out.println(sum);
    }
}

✔ 1088

import java.util.Scanner;

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

✔ 1089

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int d = sc.nextInt();
        int n = sc.nextInt();
        if (n == 1) {
            System.out.println(a);
            return;
        }
        int result = a;
        for (int i = 2; i <= n; i++) {
            result += d;
        }
        System.out.print(result);
    }
}

✔ 1090

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int r = sc.nextInt();
        int n = sc.nextInt();
        if (n == 1) {
            System.out.println(a);
            return;
        }
        long result = a;
        for (int i = 2; i <= n; i++) {
            result *= r;
        }
        System.out.print(result);
    }
}

수가 커질 경우 return의 제한 범위를 넘을 수 있기 때문에 자료형을 long으로 했다.

profile
차근차근 하나씩

0개의 댓글