"카타 8급 Closest elevator"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(사용자 3명의 중간 판 7개는 보이지 않습니다)
1번째 줄: 1번째 줄:
==개요==
==C==
{{카타 헤더}}
{{카타|8급|C|9}}
{{카타 8급-1}}
<syntaxhighlight lang='c'>
|}
const char* elevator(int left, int right, int call) {
  int L = left-call;
  int R = right-call;
  if( L < 0 ) L = -L;
  if( R < 0 ) R = -R;
  if( L < R ) return "left";
  return "right";
}
</syntaxhighlight>
<syntaxhighlight lang='c'>
const char* elevator(int left, int right, int call) {
  return abs(call - left) < abs(call - right) ? "left" : "right";
}
</syntaxhighlight>


==C++==
==C++==
[[분류: C++]]
<syntaxhighlight lang='cpp'>
<source lang='cpp'>
char* elevator(int left, int right, int call) {
char* elevator(int left, int right, int call) {
   int L = left-call;
   int L = left-call;
15번째 줄: 27번째 줄:
   return "right";
   return "right";
}
}
</source>
</syntaxhighlight>
<source lang='cpp'>
<syntaxhighlight lang='cpp'>
#include <math.h>
#include <math.h>
char* elevator(int left, int right, int call) {
char* elevator(int left, int right, int call) {
   return (abs(call-left) < abs(call-right)) ? "left" : "right";
   return (abs(call-left) < abs(call-right)) ? "left" : "right";
}
}
</source>
</syntaxhighlight>
 
==Kotlin==
{{카타|8급|Kotlin|3}}
<syntaxhighlight lang='kotlin'>
fun elevator(left: Int, right: Int, call: Int) = if (Math.abs(left-call) < Math.abs(right-call)) "left" else "right"
</syntaxhighlight>
<syntaxhighlight lang='kotlin'>
fun elevator(left: Int, right: Int, call: Int) = when {
    right == call -> "right"
    left == call -> "left"
    (Math.abs(left - call) < Math.abs(right - call)) -> "left"
    else -> "right"
}
</syntaxhighlight>
<syntaxhighlight lang='kotlin'>
fun elevator(left: Int, right: Int, call: Int) = when {
  right < left && left < call -> "left"
  call < left && left < right -> "left"
  left == call && right != call -> "left"
  else -> "right"
}
</syntaxhighlight>


==Python==
==Python==
[[분류: Python]]
<syntaxhighlight lang='python'>
<source lang='python'>
def elevator(left, right, call):
def elevator(left, right, call):
     if abs(left-call)<abs(right-call):
     if abs(left-call)<abs(right-call):
         return "left";
         return "left";
     return "right";
     return "right";
</source>
</syntaxhighlight>

2020년 11월 2일 (월) 02:41 기준 최신판

1 C[ | ]

const char* elevator(int left, int right, int call) {
  int L = left-call;
  int R = right-call;
  if( L < 0 ) L = -L;
  if( R < 0 ) R = -R;
  if( L < R ) return "left";
  return "right";
}
const char* elevator(int left, int right, int call) {
  return abs(call - left) < abs(call - right) ? "left" : "right";
}

2 C++[ | ]

char* elevator(int left, int right, int call) {
  int L = left-call;
  int R = right-call;
  if( L < 0 ) L = -L;
  if( R < 0 ) R = -R;
  if( L < R ) return "left";
  return "right";
}
#include <math.h>
char* elevator(int left, int right, int call) {
  return (abs(call-left) < abs(call-right)) ? "left" : "right";
}

3 Kotlin[ | ]

fun elevator(left: Int, right: Int, call: Int) = if (Math.abs(left-call) < Math.abs(right-call)) "left" else "right"
fun elevator(left: Int, right: Int, call: Int) = when {
    right == call -> "right"
    left == call -> "left"
    (Math.abs(left - call) < Math.abs(right - call)) -> "left"
    else -> "right"
}
fun elevator(left: Int, right: Int, call: Int) = when {
  right < left && left < call -> "left"
  call < left && left < right -> "left"
  left == call && right != call -> "left"
  else -> "right"
}

4 Python[ | ]

def elevator(left, right, call):
    if abs(left-call)<abs(right-call):
        return "left";
    return "right";
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}