c语言随机映射,将随机数映射到pi

CJam,48 46 42 38 36字节

P`'xf+1dmr`{1$f#:!1a/0=:)W+H<.>

这是同时打印π和随机数的版本:

P_p`'xf+1dmr`_oNo{1$f#:!1a/0=:)W+H<.>

正如OP在评论中所阐明的,我没有将随机数截断为15个小数位。

说明

这个想法是将π的字符串表示形式中的每个字符变成一对该字符和x。对于随机数中的每个字符,我们交换以该字符开头的第一对字符。最后,我们输出每对的第二个字符。

P` e# Get string representation of π.

'xf+ e# Append "x" to each character.

1dmr` e# Get string representation of random number in [0,1).

{ e# For each character in that string...

1$ e# Copy the list of pairs.

f# e# For each pair, find the index of the current character. If the character is

e# not in the pair, we get -1 (truthy). If it is the first character of the pair,

e# we get 0 (falsy). If it is the second character, we get 1 (truthy).

:! e# Logical NOT for each of the results. We get a 1 for every pair we could

e# potentially swap.

1a/ e# Split around those 1s.

0= e# Keep only the first chunk.

:) e# Turn all the 0s into that chunk into 1s.

W+ e# Append a -1.

H< e# Truncate to 17 elements (the number of pairs).

.% e# Apply % pairwise. This reverses the element at the position of the -1.

}/

1f= e# Select the second character from each pair.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值