백준 TEST03_for문 사용해보기

baekjoon_java

백준_TEST03_for문 사용해보기

N찍기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.*;
class TEST03_01{
public TEST03_01(){
Scanner scan = new Scanner(System.in);

System.out.print("N값 입력 : ");
int N = scan.nextInt();

for(int i=1;i<=N;i++){
System.out.println(i);
}
}
}

public class TEST03 {
public static void main(String[] args) throws IOException {
// import java.util.*;
// Scanner scan = new Scanner(System.in);
TEST03_01 test03_01 = new TEST03_01();
}
}

기찍 N

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.*;
class TEST03_02{
public TEST03_02(){
Scanner scan = new Scanner(System.in);

System.out.print("N값 입력 : ");
int N = scan.nextInt();

for(int i=N;i>=1;i--){
System.out.println(i);
}
}
}

public class TEST03 {
public static void main(String[] args) throws IOException {
// import java.util.*;
// Scanner scan = new Scanner(System.in);
TEST03_02 test03_02 = new TEST03_02();
}
}

구구단

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.util.*;

class TEST03_03{
public TEST03_03(){
Scanner scan = new Scanner(System.in);

System.out.print("N값 입력 : ");
int N = scan.nextInt();

for(int i=1;i<=9;i++){
System.out.println(N+" * "+i+" = "+N*i);
}
}
}

public class TEST03 {
public static void main(String[] args) throws IOException {
// import java.util.*;
// Scanner scan = new Scanner(System.in);
TEST03_03 test03_03 = new TEST03_03();
}
}

별찍기-1

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.*;
class TEST03_04{
public TEST03_04(){
Scanner scan = new Scanner(System.in);

System.out.print("N값 입력 : ");
int N = scan.nextInt();

for(int i=1;i<=N;i++){
for(int j=1;j<=i;j++){
System.out.print("*");
}
System.out.println("");
}
}
}

public class TEST03 {
public static void main(String[] args) throws IOException {
// import java.util.*;
// Scanner scan = new Scanner(System.in);
TEST03_04 test03_04 = new TEST03_04();
}
}

별찍기-2

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.*;
class TEST03_05{
public TEST03_05(){
Scanner scan = new Scanner(System.in);

System.out.print("N값 입력 : ");
int N = scan.nextInt();
// int K = N-1;

for(int i=1;i<=N;i++){
/*
for(int x=K;x>=1;x--){
System.out.print(" ");
}
*/
for(int j=1;j<=N-i;j++){
System.out.print(" ") ;
}

for(int j=1;j<=i;j++){
System.out.print("*");
}
//K--;
System.out.println("");
}
}
}

public class TEST03 {
public static void main(String[] args) throws IOException {
// import java.util.*;
// Scanner scan = new Scanner(System.in);
TEST03_05 test03_05 = new TEST03_05();
}
}

별찍기-3

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.*;

class TEST03_06{
public TEST03_06(){
Scanner scan = new Scanner(System.in);

System.out.print("N값 입력 : ");
int N = scan.nextInt();

for(int i=1;i<=N;i++){

for(int j=N-i+1;j>=1;j--){
System.out.print("*");
}

System.out.println("");
}
}
}

public class TEST03 {
public static void main(String[] args) throws IOException {
// import java.util.*;
// Scanner scan = new Scanner(System.in);
TEST03_06 test03_06 = new TEST03_06();
}
}

별찍기-4

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.*;
class TEST03_07{
public TEST03_07(){
Scanner scan = new Scanner(System.in);

System.out.print("N값 입력 : ");
int N = scan.nextInt();

for(int i=0;i<N;i++){
for(int j=0;j<i;j++){
System.out.print(" ");
}
for(int j=N-i;j>0;j--){
System.out.print("*");
}
System.out.println("");
}
}
}

public class TEST03 {
public static void main(String[] args) throws IOException {
// import java.util.*;
// Scanner scan = new Scanner(System.in);
TEST03_07 test03_07 = new TEST03_07();
}
}

###2007년

-> 해결은 하였으나 더 간단하게 푸는 방법이 존재함.

-> 1번=직접 해결한 소스. 2번=해답 소스

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//1번
import java.util.*;
class TEST03_08{
public TEST03_08(){
Scanner scan = new Scanner(System.in);
int x,y,z = 0; //z는 일주일 7을 의미.

while(true){
x = scan.nextInt();
if(x>=1 && x<=12){ break; }
}

while(true){
y = scan.nextInt();
if(y>=1 && y<=31){ break; }
}
// x = 월, y = 일을 입력받는다.

//ArrayList<String> month = new ArrayList<>();
//month.add("SUN");month.add("MON");
//한개씩은 위처럼 한번에는 아래처럼 이용. Array.asList
ArrayList<String> week = new ArrayList<>(Arrays.asList("MON", "TUE", "WED", "THU", "FRI", "SAT","SUN"));

for(int i=1;i<=12;i++){
if(i==1 || i==3 || i==5|| i==7 || i==8 || i==10 || i==12){
for(int j=1;j<=31;j++){
if(i==x && j==y){
System.out.println(week.get(z).toString());
}
z++;
if(z==7){
z = 0;
}
} // 1,3,5,7,8,10,11,12 월 for문 end
}
else if(i==2){
for(int j=1;j<=28;j++){
if(i==x && j==y){
System.out.println(week.get(z).toString());
}
z++;
if(z==7){
z = 0;
}
} // 2월 for문 end
}
else{
for(int j=1;j<=30;j++){
if(i==x && j==y){
System.out.println(week.get(z).toString());
}
z++;
if(z==7){
z = 0;
}
} // 4,6,9,11 월 for문 end
} // if end
}//for-i end
}
}

public class TEST03 {
public static void main(String[] args) throws IOException {
// import java.util.*;
// Scanner scan = new Scanner(System.in);
TEST03_08 test03_08 = new TEST03_08();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//2번 - 해답
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int month = sc.nextInt();
int day = sc.nextInt();
sc.close();

int[] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
String[] dayOfTheWeeks = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};

int totalDays = day;
for (int i = 0; i < month - 1; ++i) {
totalDays += daysInMonth[i];
}

System.out.println(dayOfTheWeeks[totalDays % 7]);
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.*;
class TEST03_09{
public TEST03_09(){
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int sum = 0;

for(int i=1;i<=n;i++){
sum += i;
}
System.out.println(sum);
}
}

public class TEST03 {
public static void main(String[] args) throws IOException {
// import java.util.*;
// Scanner scan = new Scanner(System.in);
TEST03_09 test03_09 = new TEST03_09();
}
}

숫자의 합 <미해결>

-> 미해결한 이유 : Scanner 메서드로는 숫자를 1개씩 입력 받는건 불가능하다.

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.*;
class TEST03_10_1{
public TEST03_10_1(){
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int a = 0;
int sum = 0;
ArrayList<Integer> array = new ArrayList<>();

for(int i=1;i<=n;i++){
a = scan.nextInt();
array.add(a);
}

for(int i=1;i<=n;i++){
sum += array.get(i-1);
}
System.out.println(sum);
}
}
public class TEST03 {
public static void main(String[] args) throws IOException {
// import java.util.*;
// Scanner scan = new Scanner(System.in);
TEST03_10_1 test03_10_1 = new TEST03_10_1();
}
}

숫자의 합 <해결>

-> 해결법 : 문자열로 받아서 끊어서 하나하나 더하거나 아스키코드를 이용한다.

-> 여기서는 끊어서 사용하는 substring() 함수를 이용.

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.*;
class TEST03_10_2{
public TEST03_10_2(){
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
String a = scan.next();
scan.close();
int sum = 0;

for(int i=0;i<n;i++){
sum += Integer.parseInt(a.substring(i, i + 1));
}
//Integer.parseInt() 인트형으로 형변환 메서드
//substring(a,b); a=현재위치 b=까지
System.out.println(sum);
}
}

public class TEST03 {
public static void main(String[] args) throws IOException {
// import java.util.*;
// Scanner scan = new Scanner(System.in);
TEST03_10_2 test03_10_2 = new TEST03_10_2();
}
}

열 개씩 끊어 출력하기

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.*;
class TEST03_11{
public TEST03_11(){
Scanner scan = new Scanner(System.in);
String n = scan.next();
scan.close();

for(int i=1;i<=n.length();i++){
System.out.print(n.charAt(i-1));
if(i%10==0){
System.out.println("");
}
}
}
}

public class TEST03 {
public static void main(String[] args) throws IOException {
// import java.util.*;
// Scanner scan = new Scanner(System.in);
TEST03_11 test03_11 = new TEST03_11();
}
}

빠른 A+B (BufferedReader , BufferedWriter)

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
36
37
38
import java.util.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

class TEST03_12{
public TEST03_12() throws IOException{
//버퍼드리더와 버퍼드라이터를 쓰기 위한 조건 : throws NumberFormatException, IOException
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));//선언

int test_case = Integer.parseInt(br.readLine()); // 몇개 입력?
//readline은 버퍼에 입력하는것.

for(int i=0; i<test_case;i++) { //ex) 5개라 입력했으면 총 5줄의 입력을 받음.
String a[] = br.readLine().split(" ");//입력과 동시에 " "공백을 구분하여 배열을 나눈다.
bw.write("현재 입려숫자는 각각"+a[0]+" "+a[1]+"\n");

int answer = Integer.parseInt(a[0]) + Integer.parseInt(a[1]);
bw.write(answer+"\n");
// write는 버퍼에 해당 값을 저장하는것.
// flush는 버퍼에 있는 내용을 printf하는것이다.
// 여기서 한번 flush를 해버리면 버퍼의 내용을 출력과 동시에 비운다.
}
bw.flush();
bw.close();
}
}

public class TEST03 {
public static void main(String[] args) throws IOException {
// import java.util.*;
// Scanner scan = new Scanner(System.in);
TEST03_12 test03_12 = new TEST03_12();
}
}



Share