1️⃣ 2438번: 별 찍기 - 1
난이도: 브론즈
분류: 구현
링크:
코드
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int N = scanner.nextInt();
// String.format() 메서드를 사용하여서 문자열로 받도록 합니다.
// 여기서 포인트는 repeat 메소드입니다.
// repeat 메소드를 사용해서 이중 반복문을 명시적으로 사용하지 않았습니다.
// 가독성이 좋고! 직관력이 좋습니다.
for(int i = 1; i <= N; i++)
System.out.println(String.format("%s", "*").repeat(i));
}
}
팀 코드
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// 문제
// 첫째 줄에는 별 1개, 둘째 줄에는 별 2개, N번째 줄에는 별 N개를 찍는 문제
// 입력
// 첫째 줄에 N(1 ≤ N ≤ 100)이 주어진다.
// 출력
// 첫째 줄부터 N번째 줄까지 차례대로 별을 출력한다.
// 예제 입력 1
// 5
// 예제 출력 1
// *
// **
// ***
// ****
// *****
/**
* 1. 1번째 줄엔 1개, 2번째 줄에 2개, n번째 줄에 n개 출력
* 2. for 문을 활용하여 *을 index 만큼 반복하여 출력.
*/
Scanner sc = new Scanner(System.in);
int star = sc.nextInt();
// 문제 답 - 최초 풀이
for (int i = 1; i <= star; i++) {
System.out.println("*".repeat(i));
}
// 문제 답 - repeat 메소드 미사용 풀이
// for (int i = 1; i <= star; i++) {
// for (int j = 0; j < i; j++) {
// System.out.print("*");
// }
// System.out.println("");
// }
// 번외 - 역순 별
// for (int i = 1; i <= star; i++) {
// System.out.print(" ".repeat(star - i));
// System.out.println("*".repeat(i));
// }
}
}
2️⃣ 2480번: 주사위 세개
난이도: 브론즈
분류: 구현
링크
코드
풀이1 - 리팩토링
import java.util.Scanner;
public class Main {
public static void solution(int num1, int num2, int num3){
// 3개의 주사위 수가 모두 일치 하는 경우, 일치하는 수를
if (num1==num2 && num1==num3){
System.out.println(10_000 + (num1) * 1_000);
// 3개의 주사위 수가 모두 불일치하는 경우, 그중 가장 큰 수만
}else if(num1!=num2 && num1 !=num3 && num2!=num3){
int max;
if(num1>= num2 && num1 >= num3){
max = num1;
}else if(num2>=num1 && num2 > num3){
max = num2;
}else {
max = num3;
}
System.out.println((max * 100));
}else{
// 3개의 주사위 수가 2개의 수가 일치하는 경우
if(num2 == num3){
System.out.println(1_000 + (num2) * 100);
}else{
// num1 == num2 && num1 == num3 경우의 수
System.out.println(1_000 + (num1) * 100);
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int num3 = scanner.nextInt();
solution(num1, num2, num3);
scanner.close();
}
}
풀이2
package 주사위3개;
import java.util.Scanner;
public class 주사위3개버전2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
int num3 = scanner.nextInt();
// Calculate the outcome
int outcome = calculateOutcome(num1, num2, num3);
System.out.println(outcome);
scanner.close();
}
private static int calculateOutcome(int num1, int num2, int num3) {
// 주사위의 3개의 수가 모두 일치 하는 경우
if(num1 == num2 && num2 == num3)
return 10_000 + (num1 * 1_000);
// 주사위 2개의 수가 일치하는 경우
int twoDiceNum;
if(num1 == num2){
twoDiceNum = num1;
}else if(num1 == num3){
twoDiceNum = num1;
}else{
twoDiceNum = num2;
}
if(twoDiceNum > 0)
return 1000 + (twoDiceNum * 100);
// 주사위 3개의 수가 모두 다르므로, 가장 큰수의 연산을 반환
return Math.max(Math.max(num1, num2), num3) * 100;
}
}
팀 코드
import java.io.*;
/*
* if-else if 문으로 해결 방법 체택
* 세 개 다 같은 수인지 비교 및 조건 연산
* 두 개만 같을 경우 조건식 고민 및 조건 연산
* 서로 다른 수 일때 MAX 값 구하는 방법 고민 및 해결
* 출력
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
String[] inputs = br.readLine().split(" ");
int num1 = Integer.parseInt(inputs[0]);
int num2 = Integer.parseInt(inputs[1]);
int num3 = Integer.parseInt(inputs[2]);
int reward = 0;
if (num1 == num2 && num2 == num3) {
reward = 10000 + num1 * 1000;
} else if (num1 == num2 || num2 == num3 || num1 == num3) {
int sameNum = (num1 == num2) ? num1 : num3;
reward = 1000 + sameNum*100;
} else {
int maxNum = Math.max(num1,Math.max(num2,num3));
reward = maxNum *100;
}
bw.write(Integer.toString(reward));
bw.close();
}
}
3️⃣ 2675번: 문자열 반복
난이도: 브론즈
분류: 구현
링크
코드
package 문자열반복;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void repeatStringParts(List<String> inputLines) {
for (String inputLine : inputLines) {
if (inputLine == null || inputLine.length() < 3) {
continue;
}
String[] parts = inputLine.split(" ");
if (parts.length != 2) {
System.out.println("입력 형식이 잘못되었습니다."); // More informative error message
continue;
}
try {
int repetitionCount = Integer.parseInt(parts[0]);
String charactersToRepeat = parts[1];
StringBuilder repeatedString = new StringBuilder();
for (char character : charactersToRepeat.toCharArray()) {
String repeatedCharacter = String.valueOf(character).repeat(repetitionCount);
repeatedString.append(repeatedCharacter);
}
System.out.println(repeatedString.toString());
} catch (NumberFormatException e) {
System.out.println("반복 횟수가 잘못되었습니다."); // More informative error message
continue;
}
}
}
public static void main(String[] args) {
BufferedReader reader = null; // More descriptive variable name
List<String> inputLines = new ArrayList<>();
try {
reader = new BufferedReader(new InputStreamReader(System.in));
String inputLine;
while ((inputLine = reader.readLine()) != null && !inputLine.isEmpty()) {
inputLines.add(inputLine);
}
} catch (IOException e) {
System.err.println("입력값 읽기 오류: " + e.getMessage()); // More informative error message
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
System.err.println("BufferedReader closure error: " + e.getMessage()); // More informative error message
}
}
}
if (inputLines.isEmpty()) {
return;
}
repeatStringParts(inputLines);
}
}
팀 코드
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
for (int i = 0; i < T ; i++) {
int R = sc.nextInt();
String s = sc.next();
for (int j = 0; j < s.length(); j++) {
for (int k = 0; k < R; k++) {
System.out.print(s.charAt(j));
}
}
System.out.println();
}
}
}
4️⃣ 2445번: 별 찍기 - 8
난이도: 브론즈
분류: 구현
링크 :
코드
팀 코드
import java.util.*;
public class _04 {
public static void main(String[] args) {
// 예제를 보고 규칙을 유추한 뒤에 별을 찍어 보세요.
// 5
// 예제 출력 1
// * *
// ** **
// *** ***
// **** ****
// **********
// **** ****
// *** ***
// ** **
// * *
/**
* 1. n을 입력 받는다.
*
* 2. 첫번째 반목문을 통해 인덱스가 n + 1보다 작을때 까지 반복
* 2-1. 인덱스만큼 *을 출력 / 공백은 n에서 인덱스를 뺀 후 2배 만큼 출력 / 인덱스만큼 *을 출력
*
* 3. 두번째 반복문을 통해 인덱스가 n보다 작을때 까지 반복
* 3-1. n-인덱스 만큼 * 출력, 공백은 인덱스의 두배만큼 출력
*/
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i < n+1; i++) {
System.out.print("*".repeat(i));
System.out.print(" ".repeat(2 * (n-i)));
System.out.print("*".repeat(i));
System.out.println();
}
for (int j = 1; j < n; j++) {
System.out.print("*".repeat(n-j));
System.out.print(" ".repeat(2*j));
System.out.print("*".repeat(n-j));
System.out.println();
}
}
}
5️⃣ 1924번: 2007년
난이도: 브론즈
분류: 구현
링크
코드
/**
* 2007년의 날짜 정보를 입력받아 요일 출력하기
*
* @author ( 작성자명 입력 )
* @since ( 작성된 날짜 입력 )
*/
package 이천칠년; // 패키지명: 2007년
import java.time.LocalDate; // 날짜 다루기 위한 클래스
import java.util.Scanner; // 사용자 입력 받기 위한 클래스
public class Main {
/**
* 프로그램의 진입점 메서드
*
* @param args 실행 시 입력되는 인자 (사용하지 않음)
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // 스캐너 객체 생성 (입력 받기 위해)
System.out.print("첫 번째 숫자 (월): ");
int num1 = scanner.nextInt(); // 정수 입력 받기 (첫 번째 숫자)
System.out.print("두 번째 숫자 (일): ");
int num2 = scanner.nextInt(); // 정수 입력 받기 (두 번째 숫자)
solution(num1, num2); // solution 메서드 호출 (입력값 전달)
scanner.close(); // 스캐너 객체 닫기 (리소스 해제)
}
/**
* 입력받은 월과 일 정보를 이용하여 2007년의 날짜 객체를 생성하고 요일 출력
*
* @param num1 월 (정수)
* @param num2 일 (정수)
*/
private static void solution(int num1, int num2) {
LocalDate date = LocalDate.of(2007, num1, num2); // 2007년 num1월 num2일 객체 생성
System.out.println("요일: " + date.getDayOfWeek().toString().substring(0, 3)); // 요일 출력 (처음 3글자만)
}
}
팀 코드
'스케쥴 > 스터디' 카테고리의 다른 글
[항해99 취업리부트 TIL] 2주차 3일 (0) | 2024.05.31 |
---|---|
[항해99 취업 리부트 TIL] 2주차 2일 (1) | 2024.05.30 |
[항해99 취업 리부트 TIL] 1주차 6일 (0) | 2024.05.28 |
[항해99 취업리부트 TIL] 1주차 5일 (0) | 2024.05.27 |
[항해99 취업리부트 TIL] 1주차 4일 (0) | 2024.05.25 |