2741번: N 찍기
https://www.acmicpc.net/problem/2741
# 코드
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
for (int i = 1; i <= n; i++) {
System.out.println(i);
}
}
}
10872번: 팩토리얼
https://www.acmicpc.net/problem/10872
# 코드
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
int result = 1;
if (n == 0) { // N이 0일 때
System.out.println(result);
} else { // N이 0이 아닐 때
for (int i = 1; i <= n; i++) {
result *= i;
}
System.out.println(result);
}
}
}
10950번: A+B (3)
https://www.acmicpc.net/problem/10950
# 코드
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long t = sc.nextLong();
// 리스트 생성
List<Long> list = new ArrayList<>();
// 테스트 케이스마다 더한 결과를 리스트에 저장
for (int i = 1; i <= t; i++) {
long a = sc.nextLong();
long b = sc.nextLong();
list.add(a + b);
}
// 리스트를 조회하여 결과 출력
for (Long ele : list) {
System.out.println(ele);
}
}
}
- 리스트 생성하기 (배열로도 가능)
- for문을 이용해 원소 조회하기
10952번: A+B (5)
https://www.acmicpc.net/problem/10952
# 코드
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 리스트 생성
List<Long> list = new ArrayList<>();
long a;
long b;
// a와 b가 모두 0이 아니라면 반복
do {
a = sc.nextLong();
b = sc.nextLong();
list.add(a + b);
} while (a != 0 && b != 0) ;
// 리스트 마지막에 추가된 0을 제외하고 결과 출력
for (int i = 0; i < list.size() - 1; i++) {
System.out.println(list.get(i));
}
}
}
2739번: 구구단
https://www.acmicpc.net/problem/2739
# 코드
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 <= 9; i++) {
System.out.println(n + " * " + i + " = " + n*i);
}
}
}
2438번: 별 찍기 (1)
https://www.acmicpc.net/problem/2438
# 코드
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++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
- cf) 파이썬은 print("*" * i) 이 가능하지만 자바는 아님.
- 아래는 파이썬으로 동일한 문제를 해결하는 코드이다. (부럽다)
n = int(input())
for i in range(1, (n + 1)):
print("*" * i)
10951번: A+B (4)
https://www.acmicpc.net/problem/10951
# 코드
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// 리스트 생성
List<Long> list = new ArrayList<>();
// 다음 줄이 있는 동안 반복
// (ctrl+z 나 ctrl+d 로 EOF처리하기 전까지 반복)
while(sc.hasNextLong()) {
long a = sc.nextLong();
long b = sc.nextLong();
list.add(a + b);
}
for (Long ele : list) {
System.out.println(ele);
}
}
}
- EOF (End of File) : 입력이 끝나는 지점
- Scanner 객체의 hasNext() 메소드는 EOF일 경우 false를 반환한다. (값이 있는 경우 true 반환)
- 위 실행에서 반복문을 종료시키려면 ctrl+z 나 ctrl+d 로 EOF 처리를 하여 종료해주면 된다.
'코딩테스트 > 자바 문제풀이' 카테고리의 다른 글
[새싹: 배열] 백준 10871, 10807, 5597, 2738 (0) | 2024.07.06 |
---|---|
[새싹: 빠른 입출력] 백준 15552 (0) | 2024.07.05 |
[새싹: 조건] 백준 1330, 9498, 14681, 2753, 2420 (0) | 2024.07.03 |
[새싹: 입력과 계산] 백준 1000, 1001, 10998, 10869, 1008, 11382 (0) | 2024.07.02 |
[새싹: 출력] 백준 2557, 10699, 7287, 10171, 10172, 25083 (3) | 2024.07.01 |