This is a blog.

02_쉽게 배우는 Java 알고리즘 입문 본문

JAVA/Algorithm

02_쉽게 배우는 Java 알고리즘 입문

Calcot 2023. 6. 8. 16:23

 

1. 합계 알고리즘(Sum Algorithm)

// [?] n명의 국어 점수 중에서 80점 이상인 점수의 합계

/**
 * 합계 알고리즘(Sum Algorithm) : 주어진 범위에 주어진 조건에 해당하는 자료들의 합계
 */

public class SumAlgorithm {
  public static void main(String[] args) {

    // [1] Input : n명의 국어 점수
    int[] scores = { 100, 75, 50, 37, 90, 95 };
    int sum = 0;

    // [2] Process : 합계 알고리즘 영역 : 주어진 범위에 주어진 조건(필터링)
    for(int i=0; i<scores.length; i++){
      if(scores[i] >= 80){
        sum += scores[i];   // SUM
      }
    }

    // [3] Output
    System.out.println(scores.length + "명의 점수 중 80점 이상의 총점 : " + sum);
  }
}

 

 

2. 등차수열(Arithmetic Sequence)

// [?] 1부터 20까지의 정수 중 홀수의 합을 구하는 프로그램
// 1,3,5,7,9, ... => 등차수열

/**
 * 등차수열(Arithmetic Sequence) : 연속하는 두 수의 차이가 일정한 수열
 */

public class ArithmeticSequence {
  public static void main(String[] args) {
    // [1] Input
    int sum = 0;    // SUM
    
    // [2] Process
    for(int i=0; i<=20; i++){   // 주어진 범위
      if(!(i % 2 == 0)){    // 주어진 조건(필터링) : 홀수
        sum += i;   // SUM
        System.out.print(i + " ");   // SEQUENCE -> Arithmetic Sequence
      }
    }

    // [3] Output
    System.out.println("1부터 20까지의 홀수의 합 : " + sum);
  }
}

 

 

3. 개수 알고리즘(Count Algorithm)

// [?] n개의 정수 중 13의 배수의 개수(건수, 횟수)

/**
 * 개수 알고리즘(Count Algorithm) : 주어진 범위에 주어진 조건에 해당하는 자료들의 개수
 */
public class CountAlgorithm {

  public static void main(String[] args) {
    // [1] Input : n개의 데이터
    int[] numbers = { 11, 12, 13, 13, 15, 13 };
    int count = 0;  // 개수를 저장할 변수는 0으로 초기화

    // [2] Process : 개수 알고리즘 영역 : 주어진 범위에 주어진 조건(필터링)
    for(int i=0; i<numbers.length; i++){
      if(numbers[i] % 13 == 0){
        count++;
      }
    }

    // [3] Output
    System.out.println(numbers.length + "개의 정수 중 13의 배수의 개수는 : "+ count);
  }

}

 

 

4. 평균 알고리즘(Average Algorithm)

// [?] n명의 점수 중에서 80점 이상 95점 이하인 점수의 평균

/**
 * 평균 알고리즘(Average Algorithm) : 주어진 범위에 주어진 조건에 해당하는 자료들의 평균
 */

public class AverageAlgorithm {
  
  public static void main(String[] args) {
    // [1] 입력 : n명의 성적
    int[] data = { 90, 65, 78, 50, 95};
    int sum = 0; // 합계 담는 그릇
    int count = 0; // 개수 담는 그릇

    // [2] 처리 : AVG = SUM / COUNT
    for(int i=0; i<data.length; i++){
      if(data[i] <= 95 && data[i] >= 80){
        sum += data[i];
        count++;
      }
    }

    double avg = sum / (double) count;

    // [3] 출력
    System.out.println("80점 이상 95점 이하인 자료의 평균 : " + avg);
  }

}

 

 

5. 최댓값 알고리즘(Max Algorithm)

// [?] 주어진 데이터 중에서 가장 큰 값

/**
 * 최댓값 알고리즘(Max Algorithm) : (주어진 범위 + 주어진 조건)의 자료들의 가장 큰 값
 */
public class MaxAlgorithm {

  public static void main(String[] args) {
    
    // [1] Initilize
    int max = Integer.MIN_VALUE;  // 정수 형식의 데이터 중 가장 작은 값으로 초기화

    // [2] Input
    int[] numbers = { -2, -5, -3, -7, -1 };

    // [3] Process : MAX
    for(int i=0; i<numbers.length; i++){
      if(max < numbers[i]){
        max = numbers[i];   // MAX : 더 큰 값으로 할당
      }
    }

    // [4] Output
    System.out.println("최댓값 : " + max);
  }

}

 

 

6. 최솟값 알고리즘(Min Algorithm)

// [?] 주어진 데이터 중에서 가장 작은 [짝수] 값

/**
 * 최솟값 알고리즘(Min Algorithm) : (주어진 범위 + 주어진 조건)의 자료들이 가장 작은 값
 */

public class MinAlgorithm {
  public static void main(String[] args) {
    // [1] Initialize
    int min = Integer.MAX_VALUE;  // 정수 형식의 데이터 중 가장 큰 값으로 초기화
    
    // [2] Input
    // int[] numbers = { 2, 5, 3, 7, 1 };

    // 이진수로 표현 + 숫자 구분자 사용 ({2, 5, 3, 7, 1})
    int[] numbers = { 0b0010, 0B0101, 0b0011, 0B0111, 0b0000_0001 };

    // [3] Process : MIN
    for(int i=0; i < numbers.length; i++){
      if(min > numbers[i] && numbers[i] % 2 == 0){
        min = numbers[i];   // MIN : 더 작은 값으로 할당
      }
    }

    // [4] Output
    System.out.println("짝수 최솟값 : " + min);
  }
}

 

 

https://me2.do/5grNjpcD

'JAVA > Algorithm' 카테고리의 다른 글

03_쉽게 배우는 Java 알고리즘 입문  (0) 2023.06.08
01_쉽게 배우는 Java 알고리즘 입문  (0) 2023.06.08
Comments