CT

[BOJ] 3단계 반복문

kinggora 2023. 1. 23. 19:34
1 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 < 10; i++){
            System.out.println(n + " * " + i + " = " + n * i);
        }
    }
}

 

2 10950 A+B - 3
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        String result = "";
        for(int i = 0; i < n; i++){
            int a = sc.nextInt();
            int b = sc.nextInt();
            result += (a+b) + "\n";
        }
        System.out.print(result);
    }
}

 

3 8393
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int sum = 0;
        for(int i = 1; i <= n; i++){
            sum += i;
        }
        System.out.print(sum);
    }
}

 

4 25304 영수증
import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int n = sc.nextInt();
        int sum = 0;
        for(int i = 0; i < n; i++){
            int a = sc.nextInt();
            int b = sc.nextInt();
            sum += a*b;
        }
        if(sum == x){
            System.out.print("Yes");
        } else{
            System.out.print("No");
        }
    }
}

 

5 25314 코딩은 체육과목 입니다
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int cnt = N/4;
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < cnt; i++) {
            sb.append("long ");
        }
        sb.append("int");
        System.out.println(sb);
    }
}

 

6 15552 빠른 A+B
import java.io.*;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        int n = Integer.parseInt(reader.readLine());
        StringTokenizer st;
        for(int i = 0; i < n; i++){
            st = new StringTokenizer(reader.readLine());
            writer.write(Integer.parseInt(st.nextToken())+Integer.parseInt(st.nextToken())+"\n");
        }
        reader.close();
        writer.flush();
        writer.close();
    }
}

*Scanner -> java.io.BufferedReader (throws IOException)

*System.out.println -> java.io.BufferedWriter (throws IOException)

*String readLine()

*java.util.StringTokenizer(str): str을 기본 delimiter로 분리

java.util.StringTokenizer(str, delim): str을 delim로 분리

 

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < n; i++){
            String str = reader.readLine();
            int index = str.indexOf(" ");
            sb.append(Integer.parseInt(str.substring(0, index)) + Integer.parseInt(str.substring(index+1))).append('\n');
        }
        reader.close();
        System.out.print(sb);
    }
}

*StringBuilder 사용

*반복문 안에서 StringTokenizer 객체를 생성하지 않아 위 방법보다 빠름

 

7 11021 A+B - 7
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        StringBuilder sb = new StringBuilder();
        for(int i = 1; i <= n; i++){
            String str = reader.readLine();
            int index = str.indexOf(" ");
            sb.append("Case #" + i + ": ");
            sb.append(Integer.parseInt(str.substring(0, index)) + Integer.parseInt(str.substring(index + 1))).append('\n');
        }
        reader.close();
        System.out.print(sb);
    }
}

 

8 11022 A+B - 8
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        StringBuilder sb = new StringBuilder();
        for(int i = 1; i <= n; i++){
            String str = reader.readLine();
            int index = str.indexOf(" ");
            int a = Integer.parseInt(str.substring(0, index));
            int b = Integer.parseInt(str.substring(index + 1));
            sb.append("Case #" + i + ": " + a + " + " + b + " = " + (a+b) + "\n");
        }
        reader.close();
        System.out.print(sb);
    }
}

 

9 2438 별 찍기 - 1
import java.io.*;

public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        StringBuilder sb = new StringBuilder();
        for(int i = 1; i <= n; i++){
            for(int j = 0; j < i; j++){
                sb.append("*");
            }
            sb.append("\n");
        }
        reader.close();
        System.out.print(sb);
    }
}

 

10 2439 별 찍기 - 2
import java.io.*;

public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        StringBuilder sb = new StringBuilder();
        for(int i = 1; i <= n; i++){
            for(int j = 0; j < n-i; j++){
                sb.append(" ");
            }
            for(int k = 0; k < i; k++){
                sb.append("*");
            }
            sb.append("\n");
        }
        reader.close();
        System.out.print(sb);
    }
}

 

11 10952 A+B - 5
import java.io.*;

public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        while(true){
            String str = reader.readLine();
            int index = str.indexOf(' ');
            int a = Integer.parseInt(str.substring(0, index));
            int b = Integer.parseInt(str.substring(index+1));
            if(a == 0 && b==0){
                break;
            }
            sb.append(a+b+"\n");
        }
        reader.close();
        System.out.print(sb);
    }
}

 

12 10951 A+B - 4
import java.io.*;

public class Main{
    public static void main(String[] args) throws IOException{
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        String str;
        while((str=reader.readLine()) != null){
            int index = str.indexOf(' ');
            int a = Integer.parseInt(str.substring(0, index));
            int b = Integer.parseInt(str.substring(index+1));
            sb.append(a+b).append("\n");
        }
        reader.close();
        System.out.print(sb);
    }
}

 

13 1110 더하기 사이클
import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int cycle = 0;
        int next = num;
        while (true){
            cycle++;
            int a = next/10;
            int b = next%10;
            next = b*10+(a+b)%10;
            if(next==num){
                break;
            }
        }
        System.out.print(cycle);
    }
}