/*문제 5 [숫자 쪼개서 가장큰수 가장 작은수 찾기]
 * 숫자(약 -21억 ~ +21억)를 입력받아서 그 숫자 중에 최대 값과 최소 값을 찾아라!
 * 예) java Test 1566874
 * 각각의 숫자 자리를 비교해라
 * 출력 예:
 * 최대 값: 8
 * 최소 값: 1

 

 

public class Test {
  public static void main(String[] args) {
    int num = Integer.parseInt(args[0]); //예)57822447
    int maxNum = num % 10, // 7
        minNum = maxNum; // 7
    num /= 10; // 5782244
    int remainder = 0;
    while (num > 0) {
      remainder = num % 10;
      if (remainder > maxNum)
        maxNum = remainder;
      if (remainder < minNum)
        minNum = remainder;
      num /= 10;
    }

    System.out.printf("최대 값: %d\n", maxNum);
    System.out.printf("최소 값: %d\n", minNum);
  }
}

Posted by 안낭우훗

/* 문제: 4 [가장 큰수 가장 작은수 찾기]
 * 숫자를 입력 받아서 그 중 가장 큰 수와 가장 작은 수를 찾아라!
 * 예) java Test 278 34 12 1980 22 6
 * 출력 예:
 * 가장 큰 수: 1980
 * 가장 작은 수: 6

 

class Test{

  public static void main(String[] args) throws Exception {
    int num = Integer.parseInt(args[0]);
    int maxNum = num;
    int minNum  = num;

    for (int i = 1; i <= args.length -1; i++ ) {
      num =  Integer.parseInt(args[i]);
      if (num > maxNum) {
        maxNum = num;
      }
      if (num < minNum) {
        minNum = num;
      }

    }
    PrintValue(maxNum, minNum);
   }
   static void PrintValue(int maxNum, int minNum) {
     System.out.printf("(최대: %d)\n(최소: %d)", maxNum, minNum);
   }
}

Posted by 안낭우훗

/* 문제: 3 [숫자 쪼개서 홀짝의 갯수세기]
 * 숫자(약 -21억 ~ +21억)를 입력받아서 그 숫자에 포함된 홀수와 짝수를 센다.
 * java Test 1566874
 * 힌트: /, % 연산자를 활용하라!
 * 출력 예:
 * 짝수: 4
 * 홀수: 3

 

class Test{

  public static void main(String[] args) throws Exception {
    int num = Integer.parseInt(args[0]);
    int op = 0;
    int oddNum = 0;
    int evenNum  = 0;

    while (num > 0) {
      op = num % 10;

   // if ((num % 2) == 0)
      if ((op % 2) == 0) {
        oddNum++;
      } else if ((op % 2) != 0) {
        evenNum++;
      } else {

      }
      num /= 10;
    }
    PrintValue(oddNum, evenNum);
   }
   static void PrintValue(int oddNum, int evenNum) {
     System.out.printf("(홀수: %d)\n(짝수: %d)", oddNum, evenNum);
   }
}

Posted by 안낭우훗

/* 문제: 2 [홀짝의 갯수세기]
 * 숫자를 입력받아서 홀수 개수와 짝수 개수를 센다.
 * 출력 예:
 * 짝수: 4
 * 홀수: 3
 * 힌트:  java Test 1 5 6 6 8 7 4

 

class Test{

  public static void main(String[] args) throws Exception {
    int num = 0;
    int oddNum = 0;
    int evenNum  = 0;

    for (int i = 0; i <= args.length -1; i++) {
      num = Integer.parseInt(args[i]);
      if ((num % 2) == 0) {
        oddNum++;
      } else if ((num % 2) != 0) {
        evenNum++;
      } else {

      }
    }
    PrintValue(oddNum, evenNum);
   }
   static void PrintValue(int oddNum, int evenNum) {
     System.out.printf("(홀수: %d)\n(짝수: %d)", oddNum, evenNum);
   }
}

Posted by 안낭우훗

/* 문제: 1 [홀수 짝수]
 * 0 ~ 9까지 숫자를 출력하고 각 숫자가 홀수인지 짝수인지 출력한다.
 * tip:  switch, if, ? :
 * 출력 예:
 * 0(짝수)
 * 1(홀수)

 

//  삼항연산자 사용

 

class Test{

  public static void main(String[] args) throws Exception {
    for(int i = 0; i <= 10; i++) {
      System.out.printf("%d : (%s) \n", i, (i % 2) == 0 ? "짝수" : "홀수") ;
    }
   }
}

 

//  if 조건문

class Test{

  public static void main(String[] args) throws Exception {
    for(int i = 0; i <= 10; i++) {
      if (i % 2 == 0) {
        Print(i, "짝수");
      } else if (i % 2 != 0) {
        Print(i, "홀수");
      } else {
        System.out.printf("응?");
      }
    }
   }
   static void Print(int no, String odd_even ) {
     System.out.printf("%d (%s) \n", no, odd_even);
   }
}

 

// switch case

class Test{

  public static void main(String[] args) throws Exception {
    for(int i = 0; i <= 10; i++) {

      switch (i % 2) {
        case 0 : Print(i, "짝수");
        break;
        case 1 : Print(i, "홀수");
        break;
        default :
        break;
      }
    }
   }
   static void Print(int no, String odd_even) {
     System.out.printf("%d (%s) \n", no, odd_even);
   }
}

Posted by 안낭우훗

/* 문제: 48 [숫자 더하기]
 * e.g.) 1, 2, 4, 7, 11, 16 ... 100 까지 합을 구하라!

 

 

/* 1, 2, 4, 7, 11, 16 ... 100 까지 합 */

 

class Test{

  public static void main(String[] args) throws Exception {
    int i = 1;
    int value = 1;
    int sum = 1;

    while (value <= 100) {
      sum += value;
      value += i++;
    }

    System.out.printf("%d", sum);
   }
}

 

 

/* 1, 2, 3, 4, 5, 6 ... 100 까지 합 */

 

class Test{

  public static void main(String[] args) throws Exception {
    int value = 1;
    int sum = 1;

    while (value <= 100) {
      sum += value++;
    }

    System.out.printf("%d", sum);
   }
}

Posted by 안낭우훗

class QuickSorting{

  static int[] elements = {3,44,38,5,47,15,36,26,27,2,46,4,19,50,48};

  public static void main(String[] args) throws Exception {
    quickSort(0, elements.length - 1);
    printElement();
   }

   static void quickSort(int startIndex, int endIndex) {
     if ((endIndex - startIndex) <= 0) {
       return;
     }
    int pivotIndex = startIndex;
    int storeIndex = pivotIndex + 1;

    for (int i = pivotIndex + 1; i <= endIndex; i++) {
      if (elements[i] < elements[pivotIndex]) {
        swapIndex(i, storeIndex);
        storeIndex++;
      }
    }
    swapIndex(pivotIndex, storeIndex - 1);
    quickSort(startIndex, storeIndex - 2);
    quickSort(storeIndex, endIndex);
   }

   static void swapIndex(int foreIndex, int backIndex) {
     if (foreIndex == backIndex) {
       return;
     }
     int tmpValue = elements[foreIndex];
     elements[foreIndex] = elements[backIndex];
     elements[backIndex] = tmpValue;
   }

   static void printElement() {
     for (int element : elements) {
       System.out.printf("%d ", element);
     }
     System.out.println();
   }

}

Posted by 안낭우훗

 

import java.util.Scanner;

class BubbleSorting{

  public static void main(String[] args) throws Exception {

    int[] arr = {3, 6, 8, 4, 12, 1, 9, 7};

    int i = 0;
    int temp;

    int end = arr.length -1;
    while (end > 0) {
       i = 0;
        while (i < end) {
          if (arr[i] > arr[i+1]) {
            temp = arr[i];
            arr[i] = arr[i+1];
            arr[i+1] = temp;
          }
          i += 1;
        }
      end--;
    }
 
    for (int each : arr)
    System.out.printf("%d \n", each);


   }
}

Posted by 안낭우훗

 

Posted by 안낭우훗
이전버튼 1 2 3 이전버튼

블로그 이미지
좋은싸이트 공유, 재해석 , 공부 정리, 틀린거 알려 주세요~
안낭우훗

태그목록

공지사항

Yesterday
Today
Total

달력

 « |  » 2025.7
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

최근에 올라온 글

최근에 달린 댓글

글 보관함