단계별로 풀어보기
단계별은 @jh05013님이 관리하고 계십니다. 단계제목설명정보총 문제내가 맞은 문제1입출력과 사칙연산입력, 출력과 사칙연산을 연습해 봅시다. Hello World!112if문if문을 사용해 봅시다.53for문for문을
www.acmicpc.net
1단계 10818 최소, 최대
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
|
import java.util.Scanner;
public class Main{
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++) {
arr[i] = sc.nextInt();
}
//min
int min = arr[0];
for(int i=1; i<arr.length; i++ ) {
if(arr[i]<min) min = arr[i];
}
//max
int max = arr[0];
for(int i=1; i<arr.length; i++) {
if(arr[i]>max) max = arr[i];
}
System.out.println(min+" "+max);
}
}
|
cs |
+숏코드로 만들어보겠다고 다시 시도함
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
|
import java.util.Scanner;
public class Main{
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++) {
arr[i] = sc.nextInt();
}
sc.close();
int min = arr[0];
int max = arr[N-1];
for(int i=0; i<arr.length; i++ ) {
if(arr[i]<=min) min = arr[i];
if(arr[i]>=max) max = arr[i];
}
System.out.println(min+" "+max);
}
}
|
cs |
1 > min max 따로 찾아주는 방식
2 > min max 같이 찾아주는 방식
3 > 2에 sc.close( ) 추가
2단계 2562 최댓값
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
|
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int[] arr = new int[9];
for(int i=0; i<9; i++) {
arr[i]=sc.nextInt();
}
int max = arr[0];
int index = 0;
for(int i=0; i<9; i++) {
if(arr[i]>=max) {
max = arr[i];
index = i+1;
}
}
System.out.println(max);
System.out.println(index);
}
}
|
cs |
3단계 2577 숫자의 개수
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
31
|
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int A = sc.nextInt();
int B = sc.nextInt();
int C = sc.nextInt();
sc.close();
int ABC = A*B*C;
String sABC = Integer.toString(ABC);
char[] arr = new char[sABC.length()];
//A*B*C 값 배열로 담기
for(int i=0; i<sABC.length(); i++) {
arr[i]=sABC.charAt(i);
}
//숫자 개수 세기
for(int i=0; i<=9; i++) {
int cnt = 0;
for(int j=0; j<arr.length; j++) {
if(arr[j]-'0'==i) cnt++;
}
System.out.println(cnt);
}
}
}
|
cs |
+ 숏코드를 위해 배열로 풀지 않고 그대로 때려박았다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int A = sc.nextInt();
int B = sc.nextInt();
int C = sc.nextInt();
sc.close();
int ABC = A*B*C;
String sABC = Integer.toString(ABC);
//숫자 개수 세기
for(int i=0; i<=9; i++) {
int cnt = 0;
for(int j=0; j<sABC.length(); j++) {
if(sABC.charAt(j)-'0'==i) cnt++;
}
System.out.println(cnt);
}
}
}
|
cs |
1> 문제에서 요구한대로 배열로 품
2> 배열로 안 풀고 문자열 그대로 때려박음
4단계 3052 나머지
✅ 서로 다른 값이 몇개인지 묻는 문제 > 애초에 중복 제거해 저장하면 됨 > Map을 사용하자!
[자바 ⑪] 자바 JAVA 이론 (자료구조 / List / Set / Map / Collection) (tistory.com)
[자바 ⑪] 자바 JAVA 이론 (자료구조 / List / Set / Map / Collection)
2021.06.17 - [IT 독학/JAVA] - [자바 ①] 자바 JAVA 이론 (객체지향언어 / 변수 / 연산자) 2021.06.18 - [IT 독학/JAVA] - [자바 ②] 자바 JAVA 이론 (제어문 / 조건문 / 반복문 / 분기문 ) 2021.06.18 - [IT 독..
datamoney.tistory.com
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Set<Integer> s = new HashSet<Integer>();
//배열 나머지 저장
for(int i=0; i<10; i++ ) {
int n = sc.nextInt();
s.add(n%42);
}
sc.close();
System.out.println(s.size());
}
}
|
cs |
5단계 1546 평균
✅ 모든 점수가 최댓값으로 나눠지고 100이 곱해진다. > 최댓값 M은 무조건 100점이 됨 (세준이 이놈..)
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
|
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
double[] arr = new double[N];
double M = arr[0];
for(int i=0; i<N; i++) {
arr[i]=sc.nextInt();
if(arr[i]>=M) M=arr[i];
}
sc.close();
double sum=0;
for(int i=0; i<N; i++) {
arr[i]=arr[i]/M*100;
sum+=arr[i];
}
System.out.println(sum/N);
}
}
|
cs |
6단계 8958 OX퀴즈
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
31
32
33
34
35
|
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
String[] arr = new String[N];
//배열에 저장
for(int i=0; i<N; i++) {
arr[i]=sc.next();
}
sc.close();
//하나씩 OX퀴즈 검사
//배열 방 개수만큼 반복
for(int i=0; i<N; i++) {
int cnt=0;
int sum=0;
//배열 한 방의 길이 만큼 검사
for(int j=0; j<arr[i].length(); j++) {
//OX 퀴즈
if(arr[i].charAt(j)=='O') {
cnt++;
} else {
cnt=0;
}
sum+=cnt;
}
System.out.println(sum);
}
}
}
|
cs |
7단계 4344 평균은 넘겠지
✅ 무조건 평균을 초과되어야 비율에 포함됨
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
31
32
33
34
|
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int C = sc.nextInt();
for(int i=0; i<C; i++) {
double sum = 0;
int N = sc.nextInt();
//입력 값 담는 배열 생성
int[] arr = new int[N];
for(int j=0; j<N; j++) {
arr[j]=sc.nextInt();
sum+=arr[j];
}
double avg = (sum/N);
double cnt = 0;
//평균 넘는 비율 계산
for(int k=0; k<N; k++) {
if(arr[k]>avg) {
cnt++;
}
}
System.out.printf("%.3f%%\n",(cnt/N)*100);
}
}
}
|
cs |
'JAVA' 카테고리의 다른 글
Leetcode 배열 42 Trapping Rain Water (java) (1) | 2023.11.18 |
---|---|
Leetcode 문자열 819 Most Common Word (java) (0) | 2023.11.14 |
백준 단계별로 풀기 자바 [4단계 - while문] (0) | 2021.08.19 |
백준 단계별로 풀기 자바 [3단계 - for문] (0) | 2021.08.16 |
백준 단계별로 풀기 자바 [2단계 - if문] (0) | 2021.08.16 |
댓글