๊ด€๋ฆฌ ๋ฉ”๋‰ด

bom's happy life

[JAVA] ํ™€์ง ๊ตฌ๋ถ„ํ•˜๊ธฐ ๋ณธ๋ฌธ

Algorithm๐Ÿ’Œ/ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

[JAVA] ํ™€์ง ๊ตฌ๋ถ„ํ•˜๊ธฐ

bompeach 2023. 4. 24. 11:42

๋ฌธ์ œ ์„ค๋ช… :

์ž์—ฐ์ˆ˜ n์ด ์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์กŒ์„ ๋•Œ ๋งŒ์•ฝ n์ด ์ง์ˆ˜์ด๋ฉด "n is even"์„, 

ํ™€์ˆ˜์ด๋ฉด "n is odd"๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์„ธ์š”.

 

์ œํ•œ์‚ฌํ•ญ :

๐Ÿ”Ž 1 ≤  n  1,000

 

์ž…์ถœ๋ ฅ ์˜ˆ :

์ž…๋ ฅ #1

100

์ถœ๋ ฅ #1

100 is even

์ž…๋ ฅ #2

1

์ถœ๋ ฅ #2

1 is odd

 

 

๋ฌธ์ œ

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
    }
}

 

 

๋ฐฉ๋ฒ• 1

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        
        
        if(n % 2 == 0) {
            System.out.println("100 is even");
        } else {
             System.out.println("1 is odd");
        }
    }
}

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ–ˆ๋”๋‹ˆ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค๋งŒ ํ†ต๊ณผํ•˜๊ณ  ์ •ํ™•์„ฑ ํ…Œ์ŠคํŠธ์—์„œ ์‹คํŒจํ–ˆ๋‹ค.

 

 

 

๋ฐฉ๋ฒ• 2

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int  n = sc.nextInt();
        
        while(true) {
            if(n == 0) {
                System.out.println("0์„ ์ž…๋ ฅํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์ž…๋ ฅํ•˜์„ธ์š”.");
                n = sc.nextInt();
            } else if(n % 2 == 1) {
                System.out.println("1 is odd");
                break;
            } else if(n % 2 == 0 ) {
                System.out.println("100 is even");
                break;
            }
        }
    }
}

๋‹ค์‹œ while๋ฌธ์œผ๋กœ ์ž‘์„ฑํ–ˆ๋Š”๋ฐ ๋˜ 50%์„ผํŠธ๋งŒ ๋งž์•˜๋‹ค๊ณ  ๋‹ค์‹œ ํ’€๋ผ๊ณ  ํ•œ๋‹ค. ์–ด๋–ป๊ฒŒ ๋ฐ”๊ฟ”์•ผ ํ•˜๋Š”๊ฑธ๊นŒ?..?

 

 

 

๋ฐฉ๋ฒ• 3 

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a;
        String result;
        String msg1= "1 is odd";
        String msg2 = "100 is even";
        
        
        int  n = sc.nextInt();
        
        result = (n % 2 == 1) ? msg1 : msg2;
        
        System.out.println(result);
    }
}

์ด๋ ‡๊ฒŒ 3ํ•ญ์—ฐ์‚ฐ์œผ๋กœ ํ•ด๋„ 100% ์ •๋‹ต์€ ์•„๋‹ˆ๋‹ค...

 

 

 

.

.

.

 

 

๋ฐฉ๋ฒ• 4

import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        if(n%2==0){
            System.out.println(n + " is even");
        }
        else{
            System.out.println(n + " is odd");
        }
    }
}

์•„.. ์ด๋ ‡๊ฒŒ ์ผ์–ด์•ผ ํ•œ๋‹ค. 100๊ณผ 1์„ ๋ฐ˜๋ณตํ•ด์„œ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์ž…๋ ฅ๋ฐ›์€ ๋ณ€์ˆ˜ n์œผ๋กœ ํ‘œ๊ธฐํ•˜๊ธฐ. 

 

 

 

๋ฐฉ๋ฒ• 5

import java.util.Scanner;

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

        System.out.print(n + " is ");
        System.out.println(n%2 == 0 ? "even" : "odd");
    }
}

๋‹ค๋ฅธ ํ’€์ด ์ค‘์— ์ด๋ ‡๊ฒŒ ์ถœ๋ ฅ์— ์‚ผํ•ญ์—ฐ์‚ฐ์„ ์‚ฌ์šฉํ•œ ๊ฒƒ์ด ์ข‹์•˜๋‹ค.

 

.

.

 

๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ–ˆ๋‹ค๋ฉด ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•ด์ค˜์•ผ ํ•œ๋‹ค!