- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我收到了一个挑战,内容如下:“设计一个程序,将一个 9 位数字作为输入,其中没有数字出现两次,并产生与下一个最高数字相对应的相同 9 位数字的排列作为输出。如果不存在这样的数字,算法应该指出这一点。例如,如果输入为 781623954,则输出为 781624359。”
所以我想出了翻转索引的想法,所以检查最后一个索引和之前的索引,看看哪个更大,然后比较然后在必要时翻转,但由于某种原因我的代码无法正常工作。我只检查了最后两位数字而不是所有数字,所以如果你能帮我检查一下,如果你对如何解决这个问题有更好的想法,请分享。
input = raw_input("Enter 9 Digits: ")
x = 9
while x>0:
x-=1
if input[8] > input[7]:
temp = input[8]
input[8] == input[7]
input[7] == temp
print input
break
最佳答案
这里有一个更有效的方法,使用 14 世纪印度数学家 Narayana Pandita 的算法,可以在关于 Permutation 的维基百科文章中找到。 .这种古老的算法仍然是已知最快的按顺序生成排列的方法之一,而且它非常稳健,因为它可以正确处理包含重复元素的排列。
下面的代码包含一个简单的 test()
函数,它生成有序数字字符串的所有排列。
#! /usr/bin/env python
''' Find the next permutation in lexicographic order after a given permutation
This algorithm, due to Narayana Pandita, is from
https://en.wikipedia.org/wiki/Permutation#Generation_in_lexicographic_order
1. Find the largest index j such that a[j] < a[j + 1]. If no such index exists,
the permutation is the last permutation.
2. Find the largest index k greater than j such that a[j] < a[k].
3. Swap the value of a[j] with that of a[k].
4. Reverse the sequence from a[j + 1] up to and including the final element a[n].
Implemented in Python by PM 2Ring 2015.07.28
'''
import sys
def next_perm(a):
''' Advance permutation a to the next one in lexicographic order '''
n = len(a) - 1
#1. Find the largest index j such that a[j] < a[j + 1]
for j in range(n-1, -1, -1):
if a[j] < a[j + 1]:
break
else:
#This must be the last permutation
return False
#2. Find the largest index k greater than j such that a[j] < a[k]
v = a[j]
for k in range(n, j, -1):
if v < a[k]:
break
#3. Swap the value of a[j] with that of a[k].
a[j], a[k] = a[k], a[j]
#4. Reverse the tail of the sequence
a[j+1:] = a[j+1:][::-1]
return True
def test(n):
''' Print all permutations of an ordered numeric string (1-based) '''
a = [str(i) for i in range(1, n+1)]
i = 0
while True:
print('%2d: %s' % (i, ''.join(a)))
i += 1
if not next_perm(a):
break
def main():
s = sys.argv[1] if len(sys.argv) > 1 else '781623954'
a = list(s)
next_perm(a)
print('%s -> %s' % (s, ''.join(a)))
if __name__ == '__main__':
#test(4)
main()
关于python - 切换索引,使用显示的完全相同的 9 位数字创建下一个最高数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31655120/
我刚刚开始学习,我的一项练习需要帮助。 我需要最终用户输入每个月的降雨量。然后我需要输出平均降雨量、最高月份和最低月份以及降雨量高于平均水平的月份。 我一直在最高和最低中得到相同的数字,我不知道为什么
我试图让一排 div 都与最高的那个的高度相匹配,所以它们看起来是统一的。 我已经阅读了很多这方面的资料,但似乎找不到任何适用于跨浏览器的解决方案。 我目前使用的脚本是: var maxHei
我有一个像 [1,4,3,1,6,5,1,4,4] 的数组 这里最高元素频率是 3 ,我需要从数组中选择频率为 3 的所有元素,如上例中的 [1,4] 。 我已经尝试过这个 var count = {
我有一个学生记录列表,grades ,我想按 GPA 排序,返回前 5 个结果。由于某种原因count awk '{ if (count awk '{ if (count<=8) print $3,
我有一个用于显示博客文章的页面。在页面的开头,我使用以下 SQL 获取数据: SELECT posts.*, count(comments.post_id) as number_of_comments
我有一张 table 城市 |状态|比赛|值(value) 可以有多个相同城市/州/种族和不同值的记录。 我想创建一个新表,其中每个城市|州|种族有一条记录 与 计数(原始表中包含城市/州/种族的记录
我是一名初级 Java 程序员(例如 0 级...)。我正在做这个项目,但我已经被难住了好几天了。我可能还有很多我没有注意到的小错误。 项目是这样的: 要求用户输入从 0.00 到 100.00 的一
我已经对我的数据进行了分组。现在,我要做的是每周从“高”列中选择最高值,并从“低”列中选择最低值,然后使用最高值减去最低值得到范围。但是代码总是错误的。有人对我有想法吗? 这是我的 DataFrame
所以几个月前我在参加编程面试时,由于某种原因这个问题让我绊倒了。我可以想到几个解决方案,但其中大多数似乎效率极低。虽然多年来我一直以某种身份进行编程,但我目前正在大学攻读 CS 学位,所以我的引用点可
我已经制定了一个程序来显示给定日期的特定时间的最高和最低流行项目。该过程没有错误或异常,并且一切正常。如您所见,为了显示 Items 的第一条记录,查询重复了两次,但唯一的区别在于顺序(ASC 和 D
我正在尝试将配对的 div 设置为相同的高度。 Some text Some text Some textSome textSome textSome textSome text Som
R 提供了最大值和最小值,但除了对整个向量进行排序然后从此向量中选取值 x 之外,我没有看到一种真正快速的方法来查找顺序中的另一个值。 例如,是否有更快的方法来获取第二高值? 最佳答案 使用sort(
这是我的命令: top -b -n 1 | head -3 | tail -n 1 | awk '{ print $2 }' 我运行一个 bash 脚本来获取这些详细信息(还有平均负载和内存消耗)并将
对于这个计划,我的目标是...使用 findKth 查找最高分、最低分、中位数和平均分用户必须输入数字(输入-1以停止扫描),但他们不知道有多少个以及是否已排序但是,我在尝试执行此操作时遇到了一些问题
我正在创建这个网站: https://www.melkerhei.be/smeltkroes/index.html 左上角的标志应该是可以点击的。这是代码:
关闭。这个问题是off-topic .它目前不接受答案。 想改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 关闭 9 年前。 Improve this
我的 table 是这样的: name | var ---------------- Joe | 3 Liz | 1 Liz | 4 Joe | 2 Peter
我有这个: function sayHello() { return new Promise( resolve => { throw new Error('reject');
JSFiddle:Example 我正在寻找一种方法来使容器 div 的高度等于其最高的子级。 每个其他子项的大小都应调整为容器的高度。 如果子元素超出其宽度,我还需要容器水平滚动。 到目前为止,我已
我有一个大小为 208 的列表(208 个句子数组),它看起来像: all_words = [["this is a sentence ... "] , [" another one hello bo
我是一名优秀的程序员,十分优秀!