전체 글 (104) 썸네일형 리스트형 손코딩 회사 손코딩문제이다. 처음 봤을 땐 간단해보였지만 아직 부족해서 잘 몰랐다. 다시 공부해야할 것 같아 블로그를 써본다. import java.text.DecimalFormat; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("가격을 입력하세요: "); int price = scanner.nextInt(); DecimalFormat formatter = new DecimalFormat("#,###"); String formattedPrice = formatter.format(price); Sys.. ToString 과 String.valueOf() toString() 과 String.valueOf()는 Object값을 String형으로 변환할 때 주로 사용하는 메소드 두 메소드는 String의 형태로 값을 변환해준다. 하지만 변경할 값이 null일 경우에 차이가 있다. 두 메소드의 차이점은 Null값에 따른 NullPointerException의 발생 유무이다. toString(): null 값을 형 변환 시 NullPointerException(NPE) 발생 ---> Object값이 String이 아니여도 출력 String.valueOf(): 파라미터로 null이 오면 "null" 문자열을 출력 프로그래머스 옷가게 할인받기 class Solution { public int solution(int price) { int answer = 0; if (price >= 500000) { answer = (int)(price * 0.8); } else if (price >= 300000) { answer = (int)(price * 0.9); } else if (price >= 100000) { answer = (int)(price * 0.95); } else { answer = price; } return answer; } } 프로그래머스 배열의 평균값 구하기 class Solution { public double solution(int[] numbers) { int n = numbers.length; int a = 0; for(int i = 0; i < n; i++){ a += numbers[i]; } double answer = (double)a / n; return answer; } } 프로그래머스 피자나눠먹기3 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; } } 간단한 배열 정렬하기! public class SelectionSort { public static void selectionSort(int[] arr) { int n = arr.length; // 배열의 길이를 구합니다. // i는 배열의 첫 번째 원소부터 마지막 바로 전 원소까지 반복합니다. for (int i = 0; i < n - 1; i++) { int minIdx = i; // 현재까지 가장 작은 값을 가진 원소의 인덱스를 저장합니다. // i 다음 원소부터 마지막 원소까지 반복합니다. for (int j = i + 1; j < n; j++) { // 현재까지 가장 작은 값보다 더 작은 값을 가진 원소가 있다면, // 그 원소의 인덱스를 minIdx에 저장합니다. if (arr[j] < arr[minIdx]) { m.. 프로그래머스 피자나눠먹기2 예제 class Solution { public int solution(int n) { int answer = 1; while(true){ if(6*answer % n == 0) break; answer++; } return answer; } } } 코드리뷰 피자 한판은 6조각. 피자는 남기지 않는다. (피자 % 6 ==0) 사람수가 늘어날 경우에 모두 동일한 조각수를 먹어야 한다. class Solution { public int solution(int n) { //n 사람의 수 1~100까지 고정 int answer = 1; //answer 피자판수 일단 1판으로 저장 while(true){ // 무조건 실행시켜주는 조건 if(6*answer % n==0) break; //만약 6X피자판수는 피자조각이고 .. 프로그래머스 피자나눠먹기1 예제 class Solution { public int solution(int n) { int answer = 0; if(n % 7 == 0){ answer = n/7; }else { answer = n/7 +1; } return answer; } } 코드리뷰 class Solution { public int solution(int n) { int answer = 0;//피자판을 저장해놓을 변수 선언 if(n % 7 == 0){//피자한판에 7조각이므로 사람수 % 7 나머지가 0일 경우 피자판수를 answer에 저장 answer = n/7; }else {//피자조각과 사람 수 가 딱 나눠떨어지지 않는 모든 경우 answer = n/7 +1;//1~6명 일경우 나눈몫이 0 +1 1판, 8명일 경우 나눈몫 1 +.. 이전 1 2 3 4 5 6 ··· 13 다음