코딩 공부/Java

[스마트인재개발원]JavaFestival 21~31번 풀이

희원96 2022. 5. 31. 00:20

public class ex21 {

	public static void main(String[] args) {

		int[] point = { 92, 32, 52, 9, 81, 2, 68 };

		int min = Math.abs(point[0] - point[1]);
		int a = 0;
		int b = 0;
		for (int i = 0; i < point.length; i++) {
			for (int j = 0; j < point.length; j++) {
				if (i != j) {
					if (min > Math.abs(point[i] - point[j])) {
						min = Math.abs(point[i] - point[j]);
						a = i;
						b = j;
					}
				}

			}
		}
		System.out.println("result = " + "[" + a + "," + b + "]");
	}
}

public class ex22 {

	public static void main(String[] args) {

		for (int i = 1; i <= 5; i++) {
			for (int j = 1; j <= 5 - i; j++) {
				System.out.print(" ");
			}
			for (int k = 1; k <= i; k++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

public class ex23 {

	public static void main(String[] args) {
		
		int[][] arr = new int[5][5];
		int data = 1;
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr.length; j++) {
				arr[i][j] = data;
				data++;
			}
		}
		System.out.println("원본");
		for (int i = 0; i < arr.length; i++) {
			for (int j = 0; j < arr.length; j++) {
				System.out.print((arr[i][j]) + "\t");
			}
			System.out.println();
		}
		System.out.println();
		System.out.println("90도 회전");

		for (int i = arr.length - 1; i >= 0; i--) {
			for (int j = 0; j < arr.length; j++) {
				System.out.print((arr[j][i]) + "\t");
			}
			System.out.println();
		}
	}
}

import java.util.Scanner;

public class ex24 {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.print("N 입력 >> ");
		int n = sc.nextInt();
		System.out.print("X 입력 >> ");
		int x = sc.nextInt();
		int[] num = new int[n];
		for (int i = 0; i < num.length; i++) {
			System.out.print(i + 1 + "번째 정수 입력 >> ");
			int input = sc.nextInt();
			num[i] = input;
		}
		System.out.print("결과 >>");
		for (int i = 0; i < num.length; i++) {
			if (num[i] < x) {
				System.out.print(" " + num[i]);
			}
		}
	}
}

import java.util.Scanner;

public class ex25 {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.print("첫 자리 0을 제외한 숫자를 입력해주세요 >> ");
		String num = sc.next();
		int count = 0;
		String arr[] = num.split("");
		for (int i = 0; i < arr.length; i++) {
			switch (arr[i]) {
			case "6":
			case "9":
				count += 6;
				break;
			case "1":
				count += 2;
				break;
			case "2":
			case "3":
			case "5":
				count += 5;
				break;
			case "4":
				count += 4;
				break;
			case "7":
				count += 3;
				break;
			case "8":
				count += 7;
			}
		}
		System.out.print("대시('-')의 총 합 >> " + count);
	}
}

 

import java.util.Scanner;

public class ex26 {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		System.out.print("첫 번째 숫자 입력 >> ");
		int num1 = sc.nextInt();
		System.out.print("두 번째 숫자 입력 >> ");
		int num2 = sc.nextInt();

		System.out.println(num1 * (num2 % 10));
		System.out.println(num1 * (num2 / 10 % 10));
		System.out.println(num1 * (num2 / 100));
		System.out.println(num1 * num2);
	}
}

import java.util.Scanner;

public class ex27 {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.println("====채점하기====");
		String answer = sc.next();
		String[] arr = answer.split("");
		int score = 0;
		int sum = 0;
		for (int i = 0; i < arr.length; i++) {

			if (arr[i].equals("o")) {
				score++;
			} else {
				score = 0;
			}
			sum += score;
		}
		System.out.println(sum);
	}
}

public class ex28 {

	public static void main(String[] args) {

		String str = "01001101";
		String[] strArr = str.split("");
		int[] arr = new int[strArr.length];

		// 쪼갠 문자열을 int형태로 변환
		for (int i = 0; i < strArr.length; i++) {
			arr[i] = Integer.parseInt(strArr[i]);
		}

		int count = 1;
		int sum = 0;

		// 2진수는 역순으로 계산하기 때문에 for문 역순으로 넣어주기
		for (int i = arr.length - 1; i >= 0; i--) {
			sum += arr[i] * count;
			count *= 2;
		}
		System.out.println(str + "(2) = " + sum + "(10)");
	}
}

import java.util.Random;
import java.util.Scanner;

public class ex29 {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		Random rd = new Random(); // 랜덤변수 선언

		int count = 1; // 실패 횟수를 세줄 변수
		while (true) {
			int num1 = rd.nextInt(9) + 1;
			int num2 = rd.nextInt(9) + 1;
			System.out.print(num1 + " + " + num2 + "= ");
			int answer = sc.nextInt();
			if (num1 + num2 == answer) {
				System.out.println("SUCCESS");
			} else {
				System.out.println("Fail...");
				count++;
			}
			if (count > 5) {
				System.out.println("GAME OVER!");
				break;
			}
		}
	}
}

import java.util.Scanner;

public class ex30 {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.println("==== 알파벳 빈도수 구하기 ====");
		int alpa[] = new int[26];

		String str = sc.nextLine();

		str = str.toLowerCase();
		str = str.replace(" ", "");
		for (int i = 0; i < str.length(); i++) {
			char ch = str.charAt(i);
			alpa[ch - 'a']++;
		}
		for (int i = 0; i < 26; i++) {
			System.out.println((char) (97 + i) + ":" + alpa[i]);
		}
	}

}

import java.util.Scanner;

public class ex31 {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.print("입력 : ");
		int num = sc.nextInt();
		int fac = 1;
		for (int i = 1; i <= num; i++) {
			fac *= i;
		}
		System.out.print("출력 : " + fac);
	}
}