์• ์ •์ฝ”๋”ฉ ๐Ÿ’ป
article thumbnail

1. ์ž์—ฐ์ˆ˜ N์ด ์ž…๋ ฅ๋˜๋ฉด 1๋ถ€ํ„ฐ N๊นŒ์ง€์˜ ์†Œ์ˆ˜์˜ ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๋งŒ์•ฝ 20์ด ์ž…๋ ฅ๋˜๋ฉด 1๋ถ€ํ„ฐ 20๊นŒ์ง€์˜ ์†Œ์ˆ˜๋Š” 2, 3, 5, 7, 11, 13, 17, 19๋กœ ์ด 8๊ฐœ์ž…๋‹ˆ๋‹ค.


์†Œ์ˆ˜๋Š” 1๊ณผ ์ž์‹ ์œผ๋กœ๋งŒ ๋‚˜๋ˆ„์–ด์ง€๋Š” ์ˆ˜ ์ž…๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฏ€๋กœ 2,3,4... ๋“ฑ ์˜ ๋ฐฐ์ˆ˜๊ฐ€ ๋˜๋ฉด ์•ˆ๋˜๊ฒ ์ฃ ! ์—ฌ๊ธฐ์„œ๋Š” int ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๋กœ 0 ์ผ๋•Œ count ๋˜๋„๋ก ํ–ˆ์Šต๋‹ˆ๋‹ค.

2๋ฒˆ์งธ for๋ฌธ์—์„œ j=j+1 ๋กœ ๋ฐฐ์ˆ˜๋“ค์„ ์ฒดํฌ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค. (์ฒดํฌํ•  ๋ฐฐ์ˆ˜ ๋งŒํผ๋งŒ for๋ฌธ์„ ๋ˆ๋‹ค)

 

import java.util.Scanner;

public class Test2_05 {
  public int solution(int number) {
    int result = 0;
    int[] ch = new int[number + 1];

    for (int i = 2; i <= number; i++) {
      // 2๋Š” ์†Œ์ˆ˜ ์ด๋ฏ€๋กœ ๋ฏธ๋ฆฌ ์ถ”๊ฐ€
      if (ch[i] == 0) {
        result++;
        // ๋ฐฐ์ˆ˜๋ฅผ ๋Œ๋ฉด์„œ ๊ฒ€์‚ฌ
        for(int j=i;j<=number;j=j+i){
          ch[j] = 1;
        }
      }
    }
    return result;
  }

  public static void main(String[] args) {
    Test2_05 main = new Test2_05();
    Scanner sc = new Scanner(System.in);
    int num = sc.nextInt();
    System.out.println(main.solution(num));
  }
}

 

2. N๊ฐœ์˜ ์ž์—ฐ์ˆ˜๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ๊ฐ ์ž์—ฐ์ˆ˜๋ฅผ ๋’ค์ง‘์€ ํ›„ ๊ทธ ๋’ค์ง‘์€ ์ˆ˜๊ฐ€ ์†Œ์ˆ˜์ด๋ฉด ๊ทธ ์†Œ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

์˜ˆ๋ฅผ ๋“ค์–ด 32๋ฅผ ๋’ค์ง‘์œผ๋ฉด 23์ด๊ณ , 23์€ ์†Œ์ˆ˜์ด๋‹ค. ๊ทธ๋Ÿฌ๋ฉด 23์„ ์ถœ๋ ฅํ•œ๋‹ค. ๋‹จ 910๋ฅผ ๋’ค์ง‘์œผ๋ฉด 19๋กœ ์ˆซ์žํ™” ํ•ด์•ผ ํ•œ๋‹ค.

์ฒซ ์ž๋ฆฌ๋ถ€ํ„ฐ์˜ ์—ฐ์†๋œ 0์€ ๋ฌด์‹œํ•œ๋‹ค.


์ž์—ฐ์ˆ˜๋ฅผ ๊ฑฐ๊พธ๋กœ ๋’ค์ง‘๋Š” ๋ฐฉ๋ฒ•

1. ๊ฑฐ๊พธ๋กœ ๋’ค์ง‘์„ ์ž์—ฐ์ˆ˜์˜ ๋์ž๋ฆฌ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•ด %10 ์„ ํ•ด์ฃผ์–ด ๋‚˜๋จธ์ง€๋ฅผ ๊ตฌํ•œ๋‹ค.

2. ๊ตฌํ•œ ๋์ž๋ฆฌ ์ˆ˜๋ฅผ ๋งจ ์•ž์— ๋‘๊ธฐ์œ„ํ•ด *10 ์„ ํ•ด์ฃผ์–ด ์œ„์น˜ํ•œ๋‹ค.

3. ๊ณ„์‚ฐ๋œ ํ•ด๋‹น ์ˆซ์ž๋ฅผ ์ž๋ฅด๊ธฐ ์œ„ํ•ด /10์„ ํ•ด์ค€๋‹ค.

 

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

  public boolean isPrime(int num) {
    if (num == 1) return false;
    for (int i = 2; i < num / 2; i++) {
      if (num % i == 0) return false;
    }
    return true;
  }

  public ArrayList<Integer> solution(int[] numberArray) {
    ArrayList<Integer> result = new ArrayList<>();
    for (int i = 0; i < numberArray.length; i++) {
      int number = 0;
      while (numberArray[i] > 0) {
        number = number * 10 + numberArray[i] % 10;
        numberArray[i] /= 10;
      }
      if (isPrime(number)) result.add(number);
    }

    return result;
  }

  public static void main(String[] args) {
    Main main = new Main();
    Scanner sc = new Scanner(System.in);
    int number = sc.nextInt();
    int[] numberArray = new int[number];
    for (int i = 0; i < number; i++) {
      numberArray[i] = sc.nextInt();
    }
    for (int x : main.solution(numberArray)) {
      System.out.print(x + " ");
    }
  }
}

 

3. OX ๋ฌธ์ œ๋Š” ๋งž๊ฑฐ๋‚˜ ํ‹€๋ฆฐ ๋‘ ๊ฒฝ์šฐ์˜ ๋‹ต์„ ๊ฐ€์ง€๋Š” ๋ฌธ์ œ๋ฅผ ๋งํ•œ๋‹ค.

์—ฌ๋Ÿฌ ๊ฐœ์˜ OX ๋ฌธ์ œ๋กœ ๋งŒ๋“ค์–ด์ง„ ์‹œํ—˜์—์„œ ์—ฐ์†์ ์œผ๋กœ ๋‹ต์„ ๋งžํžˆ๋Š” ๊ฒฝ์šฐ์—๋Š” ๊ฐ€์‚ฐ์ ์„ ์ฃผ๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ ์ˆ˜ ๊ณ„์‚ฐ์„ ํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค.

1๋ฒˆ ๋ฌธ์ œ๊ฐ€ ๋งž๋Š” ๊ฒฝ์šฐ์—๋Š” 1์ ์œผ๋กœ ๊ณ„์‚ฐํ•œ๋‹ค. ์•ž์˜ ๋ฌธ์ œ์— ๋Œ€ํ•ด์„œ๋Š” ๋‹ต์„ ํ‹€๋ฆฌ๋‹ค๊ฐ€ ๋‹ต์ด ๋งž๋Š” ์ฒ˜์Œ ๋ฌธ์ œ๋Š” 1์ ์œผ๋กœ ๊ณ„์‚ฐํ•œ๋‹ค.

๋˜ํ•œ, ์—ฐ์†์œผ๋กœ ๋ฌธ์ œ์˜ ๋‹ต์ด ๋งž๋Š” ๊ฒฝ์šฐ์—์„œ ๋‘ ๋ฒˆ์งธ ๋ฌธ์ œ๋Š” 2์ , ์„ธ ๋ฒˆ์งธ ๋ฌธ์ œ๋Š” 3์ , ..., K๋ฒˆ์งธ ๋ฌธ์ œ๋Š” K์ ์œผ๋กœ ๊ณ„์‚ฐํ•œ๋‹ค. ํ‹€๋ฆฐ ๋ฌธ์ œ๋Š” 0์ ์œผ๋กœ ๊ณ„์‚ฐํ•œ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ์•„๋ž˜์™€ ๊ฐ™์ด 10 ๊ฐœ์˜ OX ๋ฌธ์ œ์—์„œ ๋‹ต์ด ๋งž์€ ๋ฌธ์ œ์˜ ๊ฒฝ์šฐ์—๋Š” 1๋กœ ํ‘œ์‹œํ•˜๊ณ , ํ‹€๋ฆฐ ๊ฒฝ์šฐ์—๋Š” 0์œผ๋กœ ํ‘œ์‹œํ•˜์˜€์„ ๋•Œ,

์ ์ˆ˜ ๊ณ„์‚ฐ์€ ์•„๋ž˜ ํ‘œ์™€ ๊ฐ™์ด ๊ณ„์‚ฐ๋˜์–ด, ์ด ์ ์ˆ˜๋Š” 1+1+2+3+1+2=10 ์ ์ด๋‹ค.

์‹œํ—˜๋ฌธ์ œ์˜ ์ฑ„์  ๊ฒฐ๊ณผ๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์ด ์ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.


0์„ ๋งŒ๋‚ฌ์„ ๋•Œ ์ถ”๊ฐ€๋˜๋Š” ์ ์ˆ˜๋ฅผ 0์œผ๋กœ ์ดˆ๊ธฐํ™” ํ•ด์ฃผ๊ธฐ ์œ„ํ•œ tmp๋ฅผ ๋งŒ๋“ค์–ด ๊ณ„์‚ฐ ํ–ˆ๋‹ค.

import java.util.Scanner;

public class Test2_07 {

  public int solution(int[] numberArray) {
    int result = 0;
    int tmp = 0;
    for (int i = 0; i < numberArray.length; i++) {
      if (numberArray[i] == 1) {
        tmp++;
        result += tmp;
      } else {
        tmp = 0;
      }
    }

    return result;
  }

  public static void main(String[] args) {
    Test2_07 main = new Test2_07();
    Scanner sc = new Scanner(System.in);

    int num = sc.nextInt();
    int[] numArray = new int[num];
    for (int i = 0; i < num; i++) {
      numArray[i] = sc.nextInt();
    }
    System.out.println(main.solution(numArray));
  }
}

 

4. N๋ช…์˜ ํ•™์ƒ์˜ ๊ตญ์–ด์ ์ˆ˜๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ๊ฐ ํ•™์ƒ์˜ ๋“ฑ์ˆ˜๋ฅผ ์ž…๋ ฅ๋œ ์ˆœ์„œ๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

๊ฐ™์€ ์ ์ˆ˜๊ฐ€ ์ž…๋ ฅ๋  ๊ฒฝ์šฐ ๋†’์€ ๋“ฑ์ˆ˜๋กœ ๋™์ผ ์ฒ˜๋ฆฌํ•œ๋‹ค.

์ฆ‰ ๊ฐ€์žฅ ๋†’์€ ์ ์ˆ˜๊ฐ€ 92์ ์ธ๋ฐ 92์ ์ด 3๋ช… ์กด์žฌํ•˜๋ฉด 1๋“ฑ์ด 3๋ช…์ด๊ณ  ๊ทธ ๋‹ค์Œ ํ•™์ƒ์€ 4๋“ฑ์ด ๋œ๋‹ค.


1๋ฒˆ์งธ ์ˆ˜๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๋‹ค์Œ ์ˆซ์ž๋ฅผ ๋น„๊ตํ•˜์—ฌ ์ž‘์„ ๋•Œ ๋งˆ๋‹ค +1 ์„ ํ•ด์ฃผ์–ด ์ˆœ์œ„๋ฅผ ์ •ํ•ด์„œ result Array์— ๋„ฃ์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

๋„ฃ์–ด์ค€ ํ›„์—๋Š” ์ดํ›„ ์ˆ˜๋ฅผ ๋น„๊ต ํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— 1๋กœ ์ดˆ๊ธฐํ™” ํ–ˆ์Šต๋‹ˆ๋‹ค.

import java.util.ArrayList;
import java.util.Scanner;

public class Test2_08 {

  public ArrayList<Integer> solution(int[] numArray) {
    ArrayList<Integer> result = new ArrayList<>();
    int resultNumber = 1;
    for (int i = 0; i < numArray.length; i++) {
      for (int j = 0; j < numArray.length; j++) {
        if (numArray[i] < numArray[j]) {
          resultNumber++;
        }
      }
      result.add(resultNumber);
      resultNumber=1;
    }

    return result;
  }

  public static void main(String[] args) {
    Test2_08 main = new Test2_08();
    Scanner sc = new Scanner(System.in);
    int num = sc.nextInt();
    int[] numberArray = new int[num];
    for (int i = 0; i < num; i++) {
      numberArray[i] = sc.nextInt();
    }
    for (int x : main.solution(numberArray)) {
      System.out.print(x + " ");
    }
  }
}

 

๋ฐ˜์‘ํ˜•