JavaChallenge
업데이트:
JavaChallenge
package com.kh.challenge.model.service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.NavigableSet;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
public class ChallengeServiceImpl implements ChallengeService{
private Scanner sc = new Scanner(System.in);
@Override
public void challenge1() {
for(int i = 10; i > 0; i--) {
System.out.print(i + " ");
}
}
@Override
public void challenge2() {
int sum = 0;
for(int i = 1; i <= 100; i++) {
sum += i;
}
System.out.println(sum);
}
@Override
public void challenge3() {
for(int i = 3; i <= 30; i+=3) {
System.out.print(i + " ");
}
}
@Override
public void challenge4() {
for(char i = 'a'; i <= 'z'; i++) {
System.out.print(i + " ");
}
}
@Override
public void challenge5() {
System.out.print("입력 1 : ");
int input1 = sc.nextInt();
System.out.print("입력 2 : ");
int input2 = sc.nextInt();
if (input1 > input2) {
int temp = input1;
input1 = input2;
input2 = temp;
}
for(int i = input1; i <= input2; i++) {
System.out.print(i + " ");
}
}
@Override
public void challenge6() {
System.out.print("정수 입력 : ");
int num = sc.nextInt();
int cnt = 0;
for(int i = 1; i <= num; i++) {
if(num % i == 0) cnt++;
}
if(cnt == 2) System.out.println("소수 입니다.");
else System.out.println("소수가 아닙니다.");
}
@Override
public void challenge7() {
for(int i = 2; i <= 9; i++) {
for(int j = 1; j <= 9; j++) {
System.out.printf("%d * %d = %d\n", i, j, i*j);
}
}
}
@Override
public void challenge8() {
for(int i = 9; i >= 2; i--) {
for(int j = 9; j >= 1; j--) {
System.out.printf("%d * %d = %d\n", i, j, i*j);
}
}
}
@Override
public void challenge9() {
for (int i = 2; i <= 9; i++) {
for (int j = 1; j <= 9; j++) {
if ((i * j) % 5 == 0)
System.out.printf("%d * %d = %d\n", i, j, i * j);
}
}
}
@Override
public void challenge10() {
System.out.print("입력 : ");
int num = sc.nextInt();
for(int i = 0; i < num; i++) {
for(int j = 0; j < num; j++) {
System.out.print("*");
}
System.out.println();
}
}
@Override
public void challenge11() {
System.out.print("입력 : ");
int num = sc.nextInt();
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
for (int k = num; k >= num - i; k--) {
System.out.print(" ");
}
System.out.println();
}
}
@Override
public void challenge12() {
System.out.print("입력 : ");
int num = sc.nextInt();
for (int i = 0; i < num; i++) {
for (int j = num; j > i; j--) {
System.out.print("*");
}
for (int k = 0; k < i; k++) {
System.out.print(" ");
}
System.out.println();
}
}
@Override
public void challenge13() {
int num = (int) (Math.random() * 50 + 1);
int count = 0;
System.out.println("num : " + num);
while (true) {
count++;
System.out.print("입력 : ");
int input = sc.nextInt();
if (num == input) {
System.out.println("정답!! / count : " + count);
break;
} else if (input > num) {
System.out.println("Down");
} else {
System.out.println("Up");
}
}
}
@Override
public void challenge14() {dd
int[] arr = new int[10];
for (int i = 0; i < arr.length; i++) {
arr[i] = (int) (Math.random() * 10 + 1);
}
System.out.print("정방향 : ");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
System.out.print("\n역방향 : ");
for (int i = arr.length - 1; i >= 0; i--) {
System.out.print(arr[i] + " ");
}
}
@Override
public void challenge15() {
Set<Integer> num = new TreeSet<Integer>();
while(num.size() < 10) {
int random = (int)(Math.random() * 100 + 1);
num.add(random);
}
for(Integer i : num) {
System.out.print(i + " ");
}
}
@Override
public void challenge16() {
Set<Integer> num = new TreeSet<Integer>();
while(num.size() < 10) {
int random = (int)(Math.random() * 100 + 1);
num.add(random);
}
NavigableSet<Integer> decendingSet = ((TreeSet<Integer>)num).descendingSet();
for(Integer i : decendingSet) {
System.out.print(i + " ");
}
}
@Override
public void challenge17() {
int[][] arr = new int[4][4];
int num = 1;
for(int i = 0; i < 4; i++) {
for(int j = 0; j < 4; j++) {
arr[j][i] = num++;
}
}
for(int i = 0; i < arr.length; i++) {
for(int j = 0; j < arr.length; j++) {
System.out.printf("%2d ", arr[i][j]);
}
System.out.println();
}
}
@Override
public void challenge18() {
System.out.println("입력 : ");
int input = sc.nextInt();
char[][] star = new char[input][input];
for (int i = 0; i < star.length; i++) {
for (int j = 0; j < star[i].length; j++) {
if (i == j) {
star[i][j] = '*';
}
if (i + j == 4) {
star[i][j] = '*';
}
System.out.print(star[i][j] + " ");
}
System.out.println();
}
}
@Override
public void challenge19() {
int[][] random = new int[4][4];
Set<Integer> sort = new LinkedHashSet<Integer>();
while (sort.size() < 16) {
int rand = (int) (Math.random() * 16 + 1);
sort.add(rand);
}
List<Integer> list = new ArrayList<Integer>(sort);
System.out.println(list);
int index = 0;
for (int i = 0; i < random.length; i++) {
for (int j = 0; j < random[i].length; j++) {
random[i][j] = list.get(index++);
}
}
int count = 0;
while (true) {
System.out.println();
for (int i = 0; i < random.length; i++) {
for (int j = 0; j < random[i].length; j++) {
System.out.print(random[i][j] + " ");
}
System.out.println();
}
if (count == 16)
break;
System.out.print("0으로 변환할 숫자 : ");
int input = sc.nextInt();
for (int i = 0; i < random.length; i++) {
for (int j = 0; j < random[i].length; j++) {
if (input == random[i][j]) {
random[i][j] = 0;
count++;
}
}
}
}
}
@Override
public void challenge20() {
System.out.print("계산해야되는 금액 : ");
int price = sc.nextInt();
System.out.print("제출한 금액: ");
int submit = sc.nextInt();
int change = submit - price;
System.out.println("거스름돈 : " + change);
System.out.println("5만원 : " + change / 50000 + "장");
change %= 50000;
System.out.println("1만원 : " + change / 10000 + "장");
change %= 10000;
System.out.println("5천원 : " + change / 5000 + "장");
change %= 5000;
System.out.println("1천원 : " + change / 1000 + "장");
change %= 1000;
System.out.println("500원 : " + change / 500 + "개");
change %= 500;
System.out.println("100원 : " + change / 100 + "개");
change %= 100;
System.out.println("50원 : " + change / 50 + "개");
change %= 50;
System.out.println("10원 : " + change / 10 + "개");
change %= 10;
}
@Override
public void challenge21() {
System.out.print("금액 입력 : ");
int money = sc.nextInt();
int[][] lotto = new int[money/1000][6];
String[] ranking = { "[낙첨]", "[낙첨]", "[낙첨]", "[5등]", "[4등]", "[3등]", "[1등]" };
int idx = 0;
for(int i = 0; i < lotto.length; i++) {
Set<Integer> temp = new TreeSet<Integer>(generateLotto());
if(idx == 6) idx = 0;
for(int lo : temp) {
lotto[i][idx++] = lo;
}
System.out.println(temp);
}
System.out.println();
System.out.println("보너스번호 출력 ");
Set<Integer> winning = new TreeSet<Integer>(generateLotto());
System.out.println("당첨 번호 : " + winning);
int random = 0;
while(winning.size() < 7) {
random = (int)(Math.random() * 45 +1 );
winning.add(random);
}
System.out.println("보너스 : " + random);
// 당첨 여부 확인
System.out.println("\n당첨 여부 확인");
String[][] strLotto = new String[lotto.length][lotto[0].length];
boolean bonusCheck = false;
// int[][] 배열 String[][]로 변환, 출력
for (int i = 0; i < strLotto.length; i++) {
int rankingCnt = 0;
for (int j = 0; j < strLotto[i].length; j++) {
for (int check : winning) {
if (lotto[i][j] == check && check != random) {
strLotto[i][j] = "(" + lotto[i][j] + ")";
rankingCnt++;
} else if(lotto[i][j] == random) {
bonusCheck = true;
}
}
if(strLotto[i][j] == null) {
strLotto[i][j] = lotto[i][j] + "";
}
System.out.printf("%5s", strLotto[i][j] + " ");
}
if(rankingCnt == 5 && bonusCheck) {
System.out.print("[2등]");
}
System.out.println(ranking[rankingCnt]);
}
}
@Override
public Set<Integer> generateLotto() {
Set<Integer> lotto = new TreeSet<Integer>();
while(lotto.size() < 6) {
int random = (int)(Math.random() * 45 + 1);
lotto.add(random);
}
return lotto;
}
}
댓글남기기