๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป ์ฝ”๋”ฉํ…Œ์ŠคํŠธ/๋ฐฑ์ค€

[๋ฐฑ์ค€/Java] ๋‹จ๊ณ„๋ณ„๋กœ ํ’€์–ด๋ณด๊ธฐ 3๋‹จ๊ณ„ ๋ฐ˜๋ณต๋ฌธ (8393 ํ•ฉ, 25314 ์˜์ˆ˜์ฆ, 25314 ์ฝ”๋”ฉ์€ ์ฒด์œก๊ณผ๋ชฉ์ž…๋‹ˆ๋‹ค, 2439 ๋ณ„์ฐ๊ธฐ-2) ๋ชจ์Œ

by ๋ฝ€์งœ๊ผฌ 2025. 1. 17.
728x90
๋ฐ˜์‘ํ˜•

 

์ž์ž˜ํ•œ ๋ฌธ์ œ๋“ค์ด ๋งŽ์•„์„œ 

๋ชฝ๋•… ์ •๋ฆฌํ•ด๋ณด์•˜๋‹ค.


๐Ÿ“ 8393 ํ•ฉ

์ˆ˜์‹์„ ์‚ฌ์šฉํ•ด์„œ ํ’€์—ˆ๋‹ค.

import java.io.IOException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();

        System.out.println( n*(n+1)/2 );
    }
}

 

for๋ฌธ์œผ๋กœ๋„ ํ’€์–ด๋ดค๋Š”๋ฐ

import java.io.IOException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int sum = 0;

        for (int i = 1; i <= n; i++) {
            sum += i;
        }

        System.out.println(sum);
    }
}

 


๐Ÿ“ 25314 ์˜์ˆ˜์ฆ

์ฒซ์งธ ์ค„์—๋Š” ์˜์ˆ˜์ฆ์— ์ ํžŒ ์ด ๊ธˆ์•ก ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
๋‘˜์งธ ์ค„์—๋Š” ์˜์ˆ˜์ฆ์— ์ ํžŒ ๊ตฌ๋งคํ•œ ๋ฌผ๊ฑด์˜ ์ข…๋ฅ˜์˜ ์ˆ˜ ์ด ์ฃผ์–ด์ง„๋‹ค.
์ดํ›„ N๊ฐœ์˜ ์ค„์—๋Š” ๊ฐ ๋ฌผ๊ฑด์˜ ๊ฐ€๊ฒฉ a์™€ ๊ฐœ์ˆ˜ b๊ฐ€ ๊ณต๋ฐฑ์„ ์‚ฌ์ด์— ๋‘๊ณ  ์ฃผ์–ด์ง„๋‹ค.

 

์ด 2+N๊ฐœ์˜ ์ค„์„ ์ž…๋ ฅ๋ฐ›์•„์•ผํ•จ.

์Šค์บ๋„ˆ๋ž‘ ๋ฒ„ํผ๊ฐ€ ์‚ด์ง ๋‹ฌ๋ผ์„œ ๋‘˜๋‹ค ์‚ฌ์šฉํ•ด๋ณด์•˜๋‹ค.

 

์Šค์บ๋„ˆ

import java.io.IOException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner scanner = new Scanner(System.in);
        int X = scanner.nextInt();
        int N = scanner.nextInt();
        int sum = 0;

        for (int i = 0; i < N; i++) {
            int a = scanner.nextInt();
            int b = scanner.nextInt();
            sum += a * b;
        }

        if (sum == X){
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

 

๋ฒ„ํผ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int X = Integer.parseInt(br.readLine());
        int N = Integer.parseInt(br.readLine());
        int sum = 0;

        for (int i = 0; i < N; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken());
            sum += a * b;
        }

        if (sum == X){
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

๐Ÿ“ 25314 ์ฝ”๋”ฉ์€ ์ฒด์œก๊ณผ๋ชฉ์ž…๋‹ˆ๋‹ค

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        int n = N/4;
        String str = "long";

        for (int i = 0; i < n-1; i++) {
            str += " long";
        }
        System.out.println(str + " int");
    }
}

๐Ÿ“ 2439 ๋ณ„์ฐ๊ธฐ-2 (140ms -> 136ms)

์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ •๋ ฌํ•ด์ค˜์•ผํ•œ๋‹ค.

๊ทธ๋ž˜์„œ ๋ฌธ์ž์—ดํฌ๋งทํŒ…์„ ์‚ฌ์šฉํ•ด์„œ ๊ณต๋ฐฑ์„ ์ถ”๊ฐ€ํ•ด์ฃผ์—ˆ๋‹ค.

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        String str ="";
        for (int i = 0; i < N; i++) {
            str += "*";
            System.out.println(String.format("%"+N+"s",str));
        }
    }
}

 

์„ฑ๋Šฅ ๊ฐœ์„ ์„ ์œ„ํ•ด ์ถ”๊ฐ€๋กœ StringBuilder๋„ ์‚ฌ์šฉํ•ด๋ณด์•˜๋‹ค.(136ms)

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < N; i++) {
            str.append("*");
            System.out.println(String.format("%" + N + "s", str));
        }
    }
}
728x90
๋ฐ˜์‘ํ˜•