일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 핸드폰 가리기
- homebrew
- Java
- Eclipse
- sort정렬
- 프로그래머스
- 코딩테스트
- 포맷 지정자
- mysql
- 27866
- SQL코딩테스트
- 백준
- OAuth 인증
- 프로그램서
- M1
- MAC OS
- 문자열 숫자 변환
- 맥북
- Iterator
- github
- Android Studio
- 깃허브
- 알고리즘
- 안드로이드 스튜디오
- HashMap
- 자바
- JDK
- 노선별 평균 역 사이 거리 조회하기
- 해시
- 가격이 제일 비싼 식품의 정보 출력하기
Archives
- Today
- Total
개발일지
[백준 JAVA] 10807번: 개수 세기 / 10871번: X보다 작은 수 / 10818번: 최소, 최대 / 2562번: 최댓값 /5597번: 과제 안 내신 분...? 본문
Java/알고리즘 공부
[백준 JAVA] 10807번: 개수 세기 / 10871번: X보다 작은 수 / 10818번: 최소, 최대 / 2562번: 최댓값 /5597번: 과제 안 내신 분...?
O'mil 2023. 1. 31. 16:22728x90
#10807번: 개수 세기
문제
풀이
import java.util.Scanner;
public class Main_10807 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt(); // N개의 정수
int[] arr = new int[N];
for ( int i = 0; i < N; i++) {
int a = in.nextInt();
arr[i] = a;
}
int V = in.nextInt(); // 찾을 정수
int cnt = 0; // 같은 정수의 개수
for ( int j = 0; j < N; j++) {
if ( arr[j] == V) {
cnt++;
}
}
in.close();
System.out.println(cnt);
}
}
- 총정수의 개수를 입력받는다.
- N개의 정수를 입력받기 위한 배열을 선언한다. (배열의 길이는 총정수의 개수(N) 개다.)
- 반복문을 통해 입력받은 수를 배열에 삽입한다.
- 반복문을 통해 배열의 j번째 숫자와 V가 같은지 판단한다.
- 만약 두 수가 같다면 cnt의 값을 올린다.
#10871번: X보다 작은 수
문제
풀이
import java.util.Scanner;
public class Main_10871 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int X = sc.nextInt();
int[] arr = new int[N];
int a;
for ( int i = 0; i < N; i++) {
a = sc.nextInt();
arr[i] = a;
}
for ( int i = 0; i < N; i++) {
if ( arr[i] < X) {
System.out.print(arr[i] + " ");
}
}
sc.close();
System.out.println();
}
}
- 배열에 들어온 수를 모두 넣어주고 조건문을 이용해 X보다 작을 수를 출력한다.
#10818번: 최소, 최대
문제
풀이
import java.util.Scanner;
public class Main_10818 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] arr = new int[N];
for ( int i = 0; i < N; i++) {
int a = sc.nextInt();
arr[i] = a;
}
int max = arr[0];
int min = arr[0];
for ( int i = 1; i < N; i++) {
if(arr[i] > max) {
max = arr[i];
}
if ( arr[i] < min) {
min = arr[i];
}
}
sc.close();
System.out.printf("%d %d\n", min, max);
}
}
#2562번: 최댓값
문제
풀이
import java.util.Scanner;
public class Main_2562 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a;
int[] arr = new int[9];
for (int i = 0; i < 9; i++) {
a = sc.nextInt();
arr[i] = a;
}
int max = arr[0];
int temp = 1;
for ( int i = 1; i < 9; i++) {
if ( arr[i] > max) {
max = arr[i];
temp = i + 1;
}
}
sc.close();
System.out.println(max);
System.out.println(temp);
}
}
#5597번: 과제 안 내신 분...?
문제
풀이
import java.util.Scanner;
public class Main_5597 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] student = new int[31];
for ( int i = 1; i < 29; i++) {
int sucess = sc.nextInt();
student[sucess] = 1;
}
for ( int i = 1; i < student.length; i++) {
if ( student[i] != 1) {
System.out.println(i);
}
}
sc.close();
}
}
- 배열을 선언한다.
- 반복문을 통해 28명의 과제를 제출한 학생을 입력받는다.
제출한 학생들은 각자의 자리 (배열에서 학생의 출석번호와 같은 자리)에 1이 입력된다.
- 그 후 다시 한번 반복문을 통해 1이 입력되지 않은 학생들을 차례로 출력한다.
참고)
https://velog.io/@chamominedev/백준-Baekjoon-5597번-과제-안-내신-분..-JAVA
728x90
'Java > 알고리즘 공부' 카테고리의 다른 글
[백준 JAVA] 9086번: 문자열 / Buffer, flush (0) | 2023.03.14 |
---|---|
[백준 JAVA] 27866번: 문자와 문자열 / substring() (0) | 2023.03.14 |
[백준 JAVA] 1110번: 더하기 사이클 (0) | 2023.01.31 |
[백준 JAVA] 10952번: A + B - 5 / 10951번: A + B - 4 (0) | 2023.01.30 |
[백준 JAVA] 2438번: 별 찍기 - 1 / 2439번: 별 찍기 - 2 (0) | 2023.01.30 |
Comments