给定一个28或更高的正整数N,输出一个数字列表,并将其加到N,该列表将每个数字1通过7精确地使用一次。你可以给出一个程序或函数。
这些数字可以单独出现或串连在一起,只要您不重复使用其中的每一个数字。例如,[12, 34, 56, 7]是有效的,[1, 27, 6, 4, 35]和[1234, 567]也是有效的,但[123, 34567]或[3, 2, 1476]是无效的。数字被列出的顺序并不重要。
如果不能使用1-7制作N,则返回或不输出任何内容。
这些可能会消除任何混乱:
输入
28输出
[1, 2, 3, 4, 5, 6, 7]输入
100输出
[56, 7, 4, 31, 2]输入
1234567输出
[1234567]输入
29输出
没有,29是无效的。
输入
1891输出
[1234, 657]输入
370输出
[15, 342, 7, 6]如果需要的话我会做更多的。
下面是是用这七个数字创建的所有可能的数字的巴斯特箱,由FryAmTheEggman提供。
发布于 2015-10-08 20:42:19
#iRThfqQsiR10Ts./M.pS7q天真的蛮力,在网上太慢,在我的电脑上花了大约一分钟。使用常见的“循环直到异常”模式的pyth,其中访问结果过滤的组合列表导致一个错误的不可能的数字,如29。
输出就像一个节奏曲列表,例如。
1891
[1234, 657]
100
[1, 2, 34, 56, 7]
370
[12, 345, 6, 7]这是一个粘贴,所有的10136数字,可以这样做。
发布于 2015-10-10 09:22:44
main=getLine>>=print.head.f[1..7].read
f[]0=[[]]
f b s=[n:j|(n,g)<-m b,j<-f g$s-n]
m b=[(d+10*z,g)|d<-b,(z,g)<-(0,filter(/=d)b):m(filter(/=d)b)]使用递归。
Ungolfed (337字节):
delete d = filter (/= d)
main = getLine >>= print . (`form` [1..7]) . read
form s [] | s == 0 = [[]]
form s ds | s <= 0 = []
form s ds | otherwise = [n:ns | (n, ds') <- makeNumbers ds, ns <- form (s-n) ds']
makeNumbers [] = []
makeNumbers ds = [(d + 10 * n',ds') | d <- ds, (n',ds') <- (0,delete d ds):makeNumbers (delete d ds)]发布于 2020-11-04 00:07:34
ŒṗDẎṢ⁼ʋƇ7R¤Ḣ几乎所有输入都超时。如果不存在这样的列表,则输出0。需要空输出将添加3个个字节。
这是一个14字节(17用于空输出)的答案,它的运行时间或多或少是恒定的(大约40秒)。
ŒṗDẎṢ⁼ʋƇ7R¤Ḣ - Main link. Takes n on the left
Œṗ - Integer partitions of n
ʋƇ - Keep those for which the following is true:
D - Convert to digits
Ẏ - Flatten
Ṣ - Sorted
⁼ 7R¤ - Equals [1, 2, 3, 4, 5, 6, 7]
Ḣ - Take the first list14字节版本:
7Œ!ŒṖ€ẎḌS=¥Ƈ⁸Ḣ - Main link. Takes n on the left
7 - Yield 7
Œ! - Get all permutations of [1, 2, 3, 4, 5, 6, 7]
€ - Over each:
ŒṖ - Yield it's partitions
Ẏ - Flatten to a single list of all partitions
Ḍ - Convert from digits
¥Ƈ - Keep those for which the following is true:
S - Sum
= ⁸ - Equals n
Ḣ - Take the first listhttps://codegolf.stackexchange.com/questions/60103
复制相似问题