잡동사니

반응형

질문

직무

입력으로 다음이 있습니다.

  • 양의 정수 N

그리고 다음을 출력해야합니다.

테스트 케이스

1 -> 0
2 -> 1
5 -> 1
10 -> 1
12 -> 2
20 -> 2
30 -> 3
54 -> 6
97 -> 10
100 -> 10

규칙

코드 골프이므로 바이트 단위로 가장 낮은 점수가 이깁니다!


답변1

Python 2 , 17 바이트

lambda n:(n+8)/10



답변2

젤리 , 4 바이트

Ḋm⁵L

양의 정수, N 을 받아들이는 모나 딕 링크로, 음이 아닌 정수를 생성합니다.

온라인으로 사용해보세요!

어떻게?

Ḋm⁵L - Link: integer, N                           e.g. 15
Ḋ    - dequeue (implicit range [1..N]) -> [2..N]       [2,3,4,5,6,7,8,9,10,11,12,13,14,15]
  ⁵  - literal ten                                     10
 m   - modulo slice                                    [2,12]
   L - length                                          2

대안 4 바이 터 :

+8:⁵

8을 더하고 정수를 10으로 나눕니다 ( RGS의 Python 답변 에서 처음 사용 된 것처럼).



답변3

Perl 5 -pl , 13 10 바이트

@Grimmy는 올바른 출력으로 10 바이트로 줄였습니다.

$_+=1<chop

온라인으로 사용해보세요!



답변4

APL (Dyalog Unicode) , 8 7 바이트

- 버블 러 덕분에 1 바이트

전체 프로그램

⌊.1×8+⎕

Unicode)는-시도 noreferrer"!



답변5

Haskell, 14 바이트

f x = div (x 8) 10



답변6

W , 4 바이트

8+T/

또 다른 지루한 공식 : 8을 더하고 10으로 나눕니다. (두 피연산자가 정수이면 W는 정수 나눗셈을 수행합니다.)

W d , 5 바이트

[ⁿNy|

비 압축 :

Tm2=Wk

설명

    W % For every number in the range [1 .. N]:
      % Keep all that satisfies:
Tm    % After modulo by 10,
  2=  % The result is equal to 2
     k% Find the length of that


답변7

05AB1E , 4 바이트

8+T÷

다른 모든 사람과 동일한 접근 방식.

온라인으로 사용해보기 또는 모든 테스트 사례를 확인 합니다.

(약간) 더 흥미로운 5 바이트 대안 :

LT%2¢
L€θ2¢
L2Å¿O
FNθΘO

설명 :

8+     # Add 8 to the (implicit) input-integer
  T÷   # Integer-divide it by 10
       # (after which the result is output implicitly)

L      # Push a list in the range [1, (implicit) input-integer]
 T%    # Take modulo-10 on each
       # or
 €θ    # Leave the last digit of each
   2¢  # Count the amount of 2s
       # (after which the result is output implicitly)

L      # Push a list in the range [1, (implicit) input-integer]
 2Å¿   # Check for each whether it ends with a 2 (I'm actually surprised it vectorizes..)
    O  # Sum to get the amount of truthy values

F      # Loop `N` in the range [0, (implicit) input-integer):
 N     #  Push `N`
  θ    #  Pop and leave only its last digit
   Θ   #  05AB1E trutify: check if it's exactly 1
    O  #  Sum all values on the stack together
       # (after the loop, the result is output implicitly)


답변8

Retina 0.8.2 , 15 13 바이트

.+
$*
.{2,10}

온라인으로 사용해보세요! 수정 : @Grimmy 덕분에 2 바이트가 절약되었습니다. 설명:

.+
$*

단항으로 변환하십시오.

.{2,10}

10의 배수의 수를 세고, 각각은 10에서 2로 끝나는 정수를 포함하고, 10에서 2로 끝나는 마지막 정수 하나에 충분하므로 최종 2-9에 대한 추가 일치를 계산합니다.



답변9

Jelly , 7 6 5 바이트

R%⁵ċ2

온라인으로 사용해보세요! Nick에게 감사드립니다. 케네디는 저에게 1 바이트를 저장했습니다.

작동 원리 :

R      Range from 1 to n,
 %⁵    modulo 10.
   ċ2  Then count how many of those are 2.


답변10

Powershell, 50 20 바이트

1.."$args"-match"2$"

$ args는 숫자로 전달할 인수입니다.



답변11

APL WIN, 9 바이트

정수 n에 대한 프롬프트 :

/ 2 = 10 | ⍳⎕



답변12

dc , 5 4 바이트

8+I/

온라인으로 사용해보세요!

이것은 dc "함수"입니다. 스택에서 입력을 가져온 다음 출력을 스택으로 푸시합니다. (dc는 스택 기반 언어입니다.)

호출하려면 먼저 원하는 입력을 입력하고 (스택에 푸시하려면) 위의 코드를 따른 다음 p 를 입력하여 출력을 print합니다.



답변13

Bash Unix 유틸리티, 13 바이트

dc<<<$1d8+I/p

온라인으로 사용해보세요!

입력이 인수로 전달되고 출력이 print됩니다.

(이것은 내부적으로 내 dc 답변을 사용합니다.)



답변14

C (gcc) , 17 바이트

f(n){n=(n+8)/10;}



답변15

C (gcc) , 17 바이트

f(n){n+=8;n/=10;}

대체 17 바이트 :

C (gcc) , 17 바이트

f(n){n=n/10.+.8;}

(gcc)-보십시오!

대체 17 바이트

C (gcc) , 17 바이트

f(n){n=(n+8)/10;}

온라인으로 시도해보십시오. / a>



답변16

Clojure , 28 바이트

(defn e[n](int(/(+ n 8)10)))

미 골프 :

(defn ends-in-two [n]
  (int (/ (+ n 8) 10)))

테스트 하네스 :

(println (e 1))
(println (e 2))
(println (e 5))
(println (e 10))
(println (e 12))
(println (e 20))
(println (e 30))
(println (e 54))
(println (e 97))
(println (e 100))



답변17

Julia 1.0 , 12 바이트

x->(x+810



답변18

GolfScript , 6 바이트

GolfScript는 소수점을 지원하지 않으므로 / 가 작동합니다.

~8+10/

온라인으로 사용해보세요!



답변19

Kotlin , 25 바이트

{(1..it).count{it%10==2}}

title="Kotlin-보십시오!



답변20

R , 18 15 바이트

(scan()+8)%/%10

온라인으로 사용해보세요!



답변21

Stax , 4 바이트

8+A/

실행 및 디버그

표준 더하기 8, 정수 나누기 10



답변22

Burlesque , 7 바이트

8|+10|/

RGS의 방법 사용

8|+  # Add 8 (Parse string implcit)
10|/ # Divide by 10

Burlesque, 10 바이트

riq[~GO2CN

ri   # Read int
q[~  # Boxed tail (last digit)
GO   # Generate from 1, N
2CN  # Count number of 2's


답변23

철도 , 87 바이트

$'main'
 -0(!a!)-/-(a)ia(!a!)\
#od[01]a*8(a)-\ /e-----@
@-(!a!)/      >-
  \m(a)[01]--/

사용 해보세요.

설명 :

0(!a!)         Put 0 into variable "a"
(a)ia(!a!)     Add "a" with input (one number at a time), put into variable "a"
e              check if it's EOF, then go left or right at the next junction

               if false:
[10](a)m(!a!)  multiply "a" by 10, put into variable "a"

               if true:
(a)8a[10]do#   add a with 8, then divide by 10, print the output. Fin.

The rest of symbols are tracks


답변24

Erlang (escript) , 18 바이트

f(N)->(N+8)div 10.

(escript)-보십시오!

설명

f(N)->  % Function taking N as input
(N+8)   % Add input by 8
div 10. % Floor division by 10


답변25

Pyt [sic!] , 3 바이트

마침내 올바른 언어를 찾았습니다. Vim에서 이제 삭제 된 답변이 있었지만 1 입력에 대해 빈 문자열을 반환했습니다.

8+₀

설명:

8    In fact, I have no idea whether is this language stack-based, I guess it pushes 8
 +   add that 8 to the seemingly-implicit input
  ₀  divide by 10. There are also instructions to divide by numbers from 2 to 11 :)

온라인으로 사용해보세요!



답변26

Pyth , 5 바이트

/+8QT

온라인으로 사용해보세요!

설명

/+8QT
   Q   : Variable containing evaluated input
 +8    : Add 8 to it
/   T  : Divide result of add by 10


답변27

Javascript (노드) -25 바이트

f=n=>n?(n%10==2)+f(n-1):0

!



답변28

자바스크립트 (V8) , 15 바이트

f=n=>(n+8)/10|0

(V8)-보십시오!



답변29

PHP , 18 17 바이트

<?=$argn/10+.8|0;

온라인으로 사용해보세요!

@oxgeba 덕분에 -1 바이트



답변30

Python 3, 22 18 바이트

lambda n:(n+8)//10

정수 나누기를 사용하도록 제안한 @JoKing에게 감사드립니다.



 

 

 

 

출처 : https://codegolf.stackexchange.com/questions/200363/find-the-number-of-integers-in-the-range-from-1-to-n-that-ends-with-2

반응형

이 글을 공유합시다

facebook twitter googleplus kakaoTalk kakaostory naver band