Algorithm Study

[Java Algorithm] ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค Lv.0 _ ๋ง์…ˆ์‹ ์ถœ๋ ฅํ•˜๊ธฐ

microsaurs 2024. 3. 6. 14:47
๐Ÿ‘ฟ ๋ฌธ์ œ ๐Ÿ‘ฟ
๋‘ ์ •์ˆ˜ a, b ๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ˜•ํƒœ์˜ ๊ณ„์‚ฐ์‹์„ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์„ธ์š”
a + b = c

 

[Algorithm]

1. ์—ฐ์‚ฐ์ž๋ฅผ ๋ฌธ์ž์—ด๋กœ ํ‘œํ˜„

2. System.out.printf ์‚ฌ์šฉํ•ด์„œ ํ‘œํ˜„

 

[Code]

import java.util.Scanner;

public class Solution1 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = a+b;

        System.out.println(a + " + " + b + " = " + c );
    }
}

public class Solution2 {
	public static void main(String[] args) {
    
    	Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();

        System.out.printf("%d + %d = %d",a,b,a+b);
        
    }
}

 

[+ Plus]

โญ๏ธ Java์˜ ์ถœ๋ ฅ๋ฌธ 
1. System.out.println()
โžก๏ธ ๊ด„ํ˜ธ ์•ˆ์˜ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ + ๋งˆ์ง€๋ง‰์— ์ค„๋ฐ”๊ฟˆ์„ ๋„ฃ์–ด์ฃผ๋Š” ๋ฉ”์†Œ๋“œ
2. System.out.printf()
โžก๏ธ ์„œ์‹๋ฌธ์ž์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ์ถœ๋ ฅ
printf("์„œ์‹๋ฌธ์ž์—ด", value) ํ˜•์‹์œผ๋กœ ์‚ฌ์šฉ

 

* ์„œ์‹๋ฌธ์ž
- %d : ์ •์ˆ˜    - %f : ์‹ค์ˆ˜     - %s : ๋ฌธ์ž์—ด     - %c : ๋ฌธ์ž

 

 

printf๋Š” ์ƒ๊ฐ์„ ๋ชปํ–ˆ๋Š”๋ฐ, , , ๋˜‘๋˜๊ธฐ๋“ค