본문 바로가기

CodingTest/Programmers81

Lv.0 문자열 뒤집기 class Solution { public String solution(String my_string) { String answer = ""; for(int i=my_string.length()-1; i>=0; i--){ answer+=my_string.charAt(i); } return answer; } } 배열을 이용해서 my_string에 있는 문자열에서 뒤에서부터 하나씩 answer에 붙이는 풀이 class Solution { public String solution(String my_string) { StringBuilder sb = new StringBuilder(); sb.append(my_string); sb.reverse(); return sb.toString(); } } StringBu.. 2022. 11. 21.
Lv.0 피자 나눠 먹기(1) class Solution { public int solution(int n) { return n%7==0 ? n/7 : n/7+1; } } 7로 나눈 나머지가 0이면 1판만 필요하고 그게 아니면 7로 나눈 몫에 +1해서 구한다. 2022. 11. 21.
Lv.0 피자 나눠 먹기(3) class Solution { public int solution(int slice, int n) { int answer = 1; boolean flag = true; while(flag){ if(slice*answer n보다 커서 1판 만 필요하고 그렇지 않으면 사람수보다 커질때까지만 조각수에 answer++씩 하면서 피자의 필요한 판 수를 구한다. 2022. 11. 21.
Lv.0 중복된 숫자의 갯수 class Solution { public int solution(int[] array, int n) { int answer = 0; for(int i=0; i 2022. 11. 21.
Lv.0 머쓱이보다 키 큰 사람 class Solution { public int solution(int[] array, int height) { int answer = 0; for(int i=0; i 2022. 11. 21.
Lv.0 배열의 평균값 class Solution { public double solution(int[] numbers) { double answer = 0; for(int number : numbers) { answer+=number; } return answer/numbers.length; } } 소수점 한자리까지 나오길 원하기 때문에 answer를 double형으로 만들거나 answer/number.length부분에서 한쪽에라도 double로 형변환 해줘야 한다. 2022. 11. 21.
Lv.0 양꼬치 class Solution { public int solution(int n, int k) { int answer = 0; //양꼬치 총합 answer += n*12000; //서비스 음료 합 int service = (n/10)*2000; //음료의 총합 answer += k*2000; return answer-service; } } 양꼬치의 총합과 음료의 총합을 answer에 더하고 양꼬치 10인분 당 음료 1개가 서비스이므로 (양꼬치인분/10)*2000으로 서비스로 받은 음료의 값을 service에 저장한 후 answer - service를 리턴해준다. 2022. 11. 21.
Lv.0 짝수의 합 class Solution { public int solution(int n) { int answer = 0; for(int i=2; i 2022. 11. 21.
Lv.0 구 수의 나눗셈 public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num1 = sc.nextInt(); int num2 = sc.nextInt(); //double answer = Double.parseDouble(String.format("%.0f",(double)num1/num2*1000)); //int ans = (int) answer; //System.out.print(ans); double res = (double)num1/num2*1000; int resans = (int) res; System.out.println(resans); } 처음 접근을 뒤에 소수점을 잘라내려 했는데 형변환으로 간단하게 해결되는 문제였다. 2022. 11. 21.
728x90
반응형