본문 바로가기
면접준비

면접준비_자바코딩

by haheaven 2022. 1. 22.

 

1)   1~10 3가지 방식으로 합구하기

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

// 1. while
   int a = 0;
   int whileSum = 0;
   while(a <= 10){
     whileSum += a;
     a++;
   }
   System.out.println("while의 합 = " + whileSum);

// 2. for
  int forSum=0;
   for(int n=0; n<=10; n++){
      forSum += n;
   }
   System.out.println("for의 합 = " + forSum);

// 3. 가우디  
    int gaudi = 10;   // 끝 수 
    int gaudiSum = 0;
    gaudiSum = (gaudi+1)*(qaudi/2);
    if( gaudi%2 != 0 ){  // 마지막 숫자가 홀수이면
      gaudiSum += (gaudi+1)/2;
   }
     System.out.println("가우디 합 = " + gaudiSum); 
  
    }
  }

 

2) 2~9 구구단 / 특정 수 제외한 구구단 

public class Gugudan{
 public static void main(String[] args){
     // 2~9단 구구단 
      for(int dan=2; dan<=9; dan++){
          for(int n=1; n<=9; n++){
            System.out.print(" " +dan+"x"+n+"="+(dan*n));
          }
          System.out.println("");
        }
        
        
     // 사용자가 입력한 단 중 3의 배수 제외한 구구단 
     System.out.print("2~9 중 출력하고 싶은 단을 입력하세요 >> ");
     Scanner sc = new Scanner(System.in);
     int dan2 = sc.nextInt();
     for(int n=1; n<=9; n++){
       if(n%3 != 0){
          System.out.print(" "+ dan2+"x"+n+"="+(dan2*n));
       }
     }
     
   }
 }

 

 

3) 피보나치 수열 

public class Fibonacci{
  public static void main(String[] args){
    // 배열
     int[] arr = new int[20];
	 arr[1] = 1;   arr[2] = 1;
     for(int n=3; n<arr.length;  n++){
	   arr[n] = arr[n-1] + arr[n-2];
       System.out.print(arr[n] + " ");
	 }
	 System.out.println("   ");
		
     // 배열 없이
     int fibo = 20;
     int prePre = 1;
     int pre = 1;
	 for(int n =3; n<fibo; n++){
        int num = pre + prePre;
	    System.out.print(num + " ");
		prePre = pre;
	    pre = num;
     }
    System.out.println("   ");
    
     // 재귀
     for(int n=3; n<fibo; n++){
       System.out.print(fibona(n) + " ");
     }
   } // main
    
    public static int fibona(int n){
       int result = 0;
       if(n == 1) result = 1;
       else if(n == 2) result =1;
       else if (n>=3) result = fibona(n-1)+fibona(n-2);
       return result;
    }
 } // class

 

 

4) 로또 출력

import java.util.*;
public class Lotto{
 public static void main(String[] args){
 
 
    Set<Integer> lottoSet = new HashSet<Integer>();
    while(lottoSet.size() < 6 ){
      int random = (int)(Math.random()*45+1);
      lottoSet.add(random);
    }
    // 정렬없을 땐 
    System.out.println(lottoSet.toString());
    
    // 정렬할 때는 
    ArrayList<Integer> lotto = new ArrayList<Integer>(lottoSet);
    Collections.sort(lotto);
    System.out.println(lotto.toString());
 
 }
}

 

 

5) 삼각형 만들기

import java.util.*;

public class Triangle{
  public static void main(String[] args){
    System.out.print("1~10 입력해주세요 > ");
    Scanner sc = new Scanner(System.in);
    int num = sc.nextInt();
    
    // 이등변 삼각형
    for(int i=0; i<num; i++){
      for(int j=0; j<num-i-1; i++){
         System.out.print(" ");
      }
      for(int j=0; j<i*2+1; j++){
        System.out.print("*");
      }
       System.out.println(" ");
    }
    
    // 직각삼각형 
      for(int i=0; i<num; i++){
          for(int j=0; j<num-1-i; j++){
            System.out.print(" ");
          }
          for(int j=0; j<=i; j++){
            System.out.print("*");
          }
             System.out.println(" ");
      }
    
    
  }
}

'면접준비' 카테고리의 다른 글

면접준비_Spring/JSP  (0) 2022.01.23
면접준비_자바개념  (0) 2022.01.22

댓글