코테/프로그래머스
-
삼각 달팽이코테/프로그래머스 2024. 8. 6. 20:58
class Solution { public int[] solution(int n) { // 삼각형을 표현할 2 차원 배열과 채워넣을 숫자 선언 int[][] triangle = new int[n][n]; int v = 1; // v 는 채워넣을 숫자로 숫자를 triangle 에 기록할 때마다 1 씩 증가 int x = 0; // 숫자 초기화 (0,0) 을 위해 int y = 0; while (true) { // 아래로 이동하기 while (true) { triangle[y][x] = v++; // 2 차원 배열은 y,x 로 생각해야함 ..
-
교점에 별 만들기코테/프로그래머스 2024. 8. 2. 09:25
import java.util.*;public class Solution { private static class Point { // 좌표를 나타내는 클래스 public final long x, y; // final 사용해 불변성을 가지게 하고, 생성자로 초기화 할 수 있게 함 private Point(long x, long y) { this.x = x; this.y = y; } } private Point intersection(long a1, long b1, long c1, long a2, long b2, long c2) { // 직선의 교점을 찾기 위한 메서드 // x 값 = 교점 구하는 공식 double x = (dou..
-
A 강조하기코테/프로그래머스 2024. 7. 31. 21:12
class Solution { public String solution(String myString) { myString = myString.toLowerCase(); // myString 을 다 소문자로 바꾸기 myString = myString.replaceAll("a", "A"); // a 를 A 로 바꾸기 return myString; }} 다른 사람의 풀이와 매우 흡사하게 풀었다. 다른점이 있다면 replaceAll 을 사용하였지만 다른 사람의 풀이는 replace 를 사용했다. 그러므로 둘의 차이를 블로그에 올리고 올리게 된다면 밑에 링크를 달아두겠다. 2024.07.31 - [JAVA] - replace() 와 replaceAll() 차이
-
아이스 아메리카노코테/프로그래머스 2024. 7. 30. 21:20
class Solution { public int[] solution(int money) { int answer[] = new int[2]; // 배열 answer[0] = money / 5500; answer[1] = money % 5500; return answer; }}배열중 0 번째에는 돈 / 5500 을 해서 먹을 수 있는 아메리카노 개수배열중 1 번째에는 돈 % 5500 을 해서 나머지, 즉 잔돈을 계산한 코드 다른 사람의 코드는 비슷해보이지만 매우 간결하게 작성하였다
-
피자 나눠 먹기 (3)코테/프로그래머스 2024. 7. 30. 21:04
// 내 풀이class Solution { public int solution(int slice, int n) { int answer = 0; if (n % slice == 0){ answer = n / slice; } else { answer = n / slice + 1; } return answer; }} 내 풀이는 간단한 if else 문 하지만 다른 사람의 풀이를 보면 삼항연산자를 사용하였다. 삼항연산자를 모르는 건 아니지만 익숙하지 않아 if else 문을 사용해 효율이 좋지 않았다. 틀린 건 아니지만 효율 좋은 코드를 위해 조금 더 생각해봐야겠다
-
옷가게 할인 받기코테/프로그래머스 2024. 5. 2. 21:56
class Solution { public int solution(int price) { int answer = 0; if (price >= 500000) { // 50만원보다 클때 price = (int) (price * 0.8); // 20퍼 할인 answer = price; // 20퍼 할인 가격 = answer } else if (price >= 300000) { price = (int) (price * 0.9); answer = price; } else if (price >= 100000) { price = (in..