본문 바로가기

코딩테스트/Coding Bat

[코딩뱃] [자바] Warmup - 1단계 : lastDigit 문제

728x90
728x90

https://codingbat.com/prob/p125339

 

CodingBat Java Warmup-1 lastDigit

Given two non-negative int values, return true if they have the same last digit, such as with 27 and 57. Note that the % "mod" operator computes remainders, so 17 % 10 is 7.lastDigit(7, 17) → truelastDigit(6, 17) → falselastDigit(3, 113) → trueGo...S

codingbat.com



= 문제 번역 =

음이 아닌 두 개의 int 값이 주어지면 27과 57과 같이 마지막 숫자가 같으면 true를 반환합니다. 

% "mod" 연산자는 나머지를 계산하므로 17 % 10은 7입니다.

 

= 문제푸는 팁 =

나는 문제를 풀 때, 보통 첫 줄부터 식으로 풀어가는편이라서, a<0||b<0의 조건을 붙였다

2번 해설과 같이 문제를 풀 면 식을 더 간단하게 쓸 수 있어서 좋은 것 같다.

 

= 해설 =

< 1 >
public boolean lastDigit(int a, int b) {
  if(a<0||b<0) return false;
  
  if(a%10==b%10){
    return true;
  }else return false;
}

< 2 >
public boolean lastDigit(int a, int b) {
  // True if the last digits are the same
  return(a % 10 == b % 10);
}

728x90