给定输入,输出所有基数和幂加到该输入的所有指数


20

因此,这是我在此站点上遇到的第一个挑战。

面临的挑战是接受一个将为正的输入整数,并以升序(从到,包括n)打印的输出(其中是当前整数)。 n1ni(ni)i

给定输入5,程序将打印:

1  
8  
9  
4  
1  

14是1且是8且是9且是4且是1且1+4=5
232+3=5
323+2=5
414+1=5
505+0=5

输入输出

输入将采用正整数形式。输出将是一个数字列表,以逗号或换行符分隔。

这是,因此最短的代码获胜。


5
应该省略逗号/换行符的详细信息,在这里,通常的做法是让列表输出采用任何方便的格式,包括作为函数返回的列表/数组对象
Sparr 18'Nov

3
输入是否总是大于0,还是必须处理0和负数?
维斯卡

输入永远是积极的
无知的体现

6
两个同样简短的答案无关紧要。如果您想接受答案,请选择最早发布的答案。但是,强烈建议至少等待几天,并且建议不要接受答案(以鼓励提交更多内容)。
世纪

2
标题不应该是“给出一个整数,打印以基数和和为输入的指数所获得的所有 ”吗?
Nicola Sap

Answers:


6

APL(Dyalog Unicode)8 5字节

⍳*⊢-⍳

在线尝试!

匿名前缀默认功能。TIO测试范围为[1..10]。

感谢@lirtosiast 3个字节。

怎么样:

⍳*⊢-⍳  Tacit function
      Range. n generates the vector [1..n].
  ⊢-  Subtracted from the argument. The vector is now [n-1,n-2,...,0]
⍳*     Exponentiate using the range [1..n] as base. The result is the vector
       [1^(n-1), 2^(n-2), 3^(n-3),...]

2
⍳*⊢-⍳是5个字节,使用⎕IO←1
lirtosiast

@lirtosiast花了我一段时间才弄清楚为什么行得通,但我明白了。谢谢。
J.Sallé18年


5

Japt,5个字节

õ_p´U

试试吧

õ         :Range [1,input]
 _        :Map
  p       :  Raise to the power of
   ´U     :  Input decremented


5

Aheui(esotope) 193个 164字节(56个字符)

방빠싹받분샥퍼붇바파쟈뿌차샦히망맣여
타빠바푸투반또분뽀뿌서썪삯타삯받반타
석차샦져쌲볼어타토싻삭빠쏛ㅇ또섞썪뻐

在线尝试!

在AVIS韩文上试用;只需复制并粘贴上面的代码,按开始按钮,输入数字,看看它如何移动。要查看输出,请按左侧的> _图标。


它打的不是很多,但我试一下。


是否可以选择一个字符集,以便每个字符存储在2个字节中?
tsh

@tsh根据Aheui规范,Aheui代码仅包含UTF-8字符。
cobaltp

4

Pyth,5个字节

_m^-Q

在线尝试!

最佳编码为4.106字节。

_                reverse of the following list:
 m               map the following lambda d:
  ^                (N-d)**d
   -Qd             
      d
       Q         over [0,...,N-1]










2

视网膜,35字节

.+
*
_
$$.($.'*$($.>`$*)_¶
%~`^
.+¶

在线尝试!说明:

.+
*

将输入转换为一元。

_

匹配每个位置。然后设置几个替换变量。$`成为比赛的左边;$>`修改为左边并匹配;$.>`修改它以获取长度,即当前索引。$'同时是匹配的权利,$.'长度也就是当前的指数。

$$.($.'*$($.>`$*)_¶

创建一个字符串$.($.'的重复$.>`*_。例如,对于原始输入5中的索引2,其$.'值为3且$.>`为2,因此结果字符串为$.(2*2*2*_。这是一个视网膜替代表达式,可以计算2³。每个字符串在其自己的行上输出。

%~`^
.+¶

对于前一阶段生成的每一行,在其前面添加一行.+,将其变为替换阶段,然后对该阶段求值,从而计算表达式。


2

QBasic,35 33字节

谢谢@Neil的2个字节!

INPUT a
FOR b=1TO a
?b^(a-b)
NEXT

REPL.IT的版本略有扩展,因为解释器不完全符合规范。

输出量

QBasic (qb.js)
Copyright (c) 2010 Steve Hanov

   5
1
8
9
4
1

通过以正确的顺序输出列表节省2个字节!(b^(a-b)for b=1..a
Neil

@Neil谢谢,我已经努力了!
steenbergh




2

MATL,5个字节

:Gy-^

在线尝试!

说明

以输入5为例。

:     % Implicit input. Range
      % STACK: [1 2 3 4 5]
G     % Push input again
      % STACK: [1 2 3 4 5], 5
y     % Duplicate from below
      % STACK: [1 2 3 4 5], 5, [1 2 3 4 5]
-     % Subtract, element-wise
      % STACK: [1 2 3 4 5], [4 3 2 1 0]
^     % Power, element-wise. Implicit display
      % STACK: [1 8 9 4 1]

2

Java,59字节

for(int i=1;a+1>i;i++)System.out.println(Math.pow(i,a-i));

1
欢迎来到PPCG。看来这需要将“输入”分配给预定义的变量a,我们不允许这样做。
毛茸茸的

2
您好,这是为您解决的问题:n->{for(int i=0;i++<n;)System.out.println(Math.pow(i,n-i));} 60个字节(链接中的代码和测试用例)
OlivierGrégoire18年

1

干净,37字节

import StdEnv
$n=[i^(n-i)\\i<-[1..n]]

在线尝试!

定义$ :: Int -> [Int]一个整数并返回结果列表。

$ n                // function $ of n
 = [i ^ (n-i)      // i to the power of n minus i
    \\ i <- [1..n] // for each i in 1 to n
   ]


1

05AB1E,5 个字节

LD<Rm

@lirtosiast的Jelly答案端口。

在线尝试。

说明:

L      # List in the range [1, (implicit) input integer]
       #  i.e. 5 → [1,2,3,4,5]
 D<    # Duplicate this list, and subtract 1 to make the range [0, input)
       #  i.e. [1,2,3,4,5] → [0,1,2,3,4]
   R   # Reverse it to make the range (input, 0]
       #  i.e. [0,1,2,3,4] → [4,3,2,1,0]
    m  # Take the power of the numbers in the lists (at the same indices)
       # (and output implicitly)
       #  i.e. [1,2,3,4,5] and [4,3,2,1,0] → [1,8,9,4,1]


1

R,22个字节

n=scan();(1:n)^(n:1-1)

不言自明;请注意,:运算符比运算符具有更高的优先级,-因此n:1-1(n-1):0

如果允许我们从0开始,那么通过(0:n)^(n:0)避免使用-1 可以损失两个字节。


1

木炭,9字节

I⮌ENX⁻θιι

在线尝试!链接是详细版本的代码。说明:

   N        Input as a number
  E         Map over implicit range
       ι    Current value
     ⁻      Subtracted from
      θ     First input
    X       Raised to power
        ι   Current value
 ⮌          Reverse list
I           Cast to string
             Implicitly print on separate lines


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.