반응형
[ASAC 웹풀스택] 백준 2501번 3460번 10818번 2609번 2309번 2460번
[ASAC 웹풀스택] 2501번 약수 구하기
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int K = sc.nextInt();
int count = 0;
int answer = 0;
for (int i = 1; i <= N; i++) {
if (N % i == 0) {
count++;
if (count == K) {
answer = i;
break;
}
}
}
System.out.println(answer);
}
}
[ASAC 웹풀스택] 3460번 이진수 구하기
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
for (int i = 0; i < T; i++) {
int n = scanner.nextInt();
for (int j = 0; n > 0; j++) {
if ((n & 1) == 1) {
System.out.print(j + " ");
}
n >>= 1;
}
System.out.println();
}
scanner.close();
}
}
[ASAC 웹풀스택] 10818번 최소 ,최대
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T = scanner.nextInt();
for (int i = 0; i < T; i++) {
int n = scanner.nextInt();
for (int j = 0; n > 0; j++) {
if ((n & 1) == 1) {
System.out.print(j + " ");
}
n >>= 1;
}
System.out.println();
}
scanner.close();
}
}
Integer.MAX_VALUE와 Integer.MIN_VALUE는 Java에서 제공하는 Integer 클래스의 정적 상수입니다.
Integer.MAX_VALUE: int 데이터 타입이 가질 수 있는 최대값인 2^31 - 1 (약 21억)을 나타냅니다.
Integer.MIN_VALUE: int 데이터 타입이 가질 수 있는 최소값인 -2^31 (약 -21억)을 나타냅니다.
[ASAC 웹풀스택] 2460번 지능형 기차
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int maxPassengers = 0;
int currentPassengers = 0;
for (int i = 0; i < 10; i++) {
int off = scanner.nextInt();
int on = scanner.nextInt();
currentPassengers -= off;
currentPassengers += on;
if (currentPassengers > maxPassengers) {
maxPassengers = currentPassengers;
}
}
System.out.println(maxPassengers);
scanner.close();
}
}
[ASAC 웹풀스택] 2309번 일곱 난쟁이
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] dwarfs = new int[9];
for (int i = 0; i < 9; i++) {
dwarfs[i] = scanner.nextInt();
}
Arrays.sort(dwarfs);
int totalHeight = Arrays.stream(dwarfs).sum();
for (int i = 0; i < 9; i++) {
for (int j = i + 1; j < 9; j++) {
int sumExcludingTwoDwarfs = totalHeight - dwarfs[i] - dwarfs[j];
if (sumExcludingTwoDwarfs == 100) {
for (int k = 0; k < 9; k++) {
if (k != i && k != j) {
System.out.println(dwarfs[k]);
}
}
scanner.close();
return;
}
}
}
}
}
if (k != i && k != j)는 논리 AND 연산자 (&&)를 사용하여 두 개의 조건을 동시에 검사하는 것을 나타냅니다.
k != i: 현재 반복 중인 k의 값이 i와 같지 않은 경우.
k != j: 현재 반복 중인 k의 값이 j와 같지 않은 경우.
이 두 조건이 모두 참일 때, if문 안의 코드 블록이 실행됩니다. 즉, k가 동시에 i와 j와 같지 않을 때 코드가 실행되며, 이는 현재 찾은 두 명의 키 i와 j를 제외한 다른 난쟁이의 인덱스 k를 나타냅니다.
[ASAC 웹풀스택] 2609번 최대공약수와 최소공배수
2609번 최대공약수와 최소공배수
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int gcd = gcd(num1, num2);
System.out.println(gcd);
int lcm = (num1 * num2) / gcd;
System.out.println(lcm);
scanner.close();
}
public static int gcd(int a, int b) {
while (b != 0) {
int r = a % b;
a = b;
b = r;
}
return a;
}
}
반응형
댓글