使用迭代查找python中的最小k

如何解决使用迭代查找python中的最小k

我是编程的新手,我正在尝试迭代。

我想要做一个例子是:

输入一个正整数:100

19是最小的n,使得1+3+5+7+...+n >= 100

因此,从插入的整数开始,以2为步长,从1开始求和,直到总和等于插入的整数。

我收到一条错误消息

   while r <= q:
TypeError: '<=' not supported between instances of 'range' and 'int'

我不确定在不使用范围和整数的情况下如何执行此操作。 有更好的方法吗?

到目前为止,我一直在下面的代码中进行尝试,但实际上是:我试图以2的步长创建变量和s,范围从0到q,然后s小于或等于q继续通过n求和。我输入了r尝试解决我遇到的错误,但是它仍然给出相同的错误

也许我在想这是错误方法,对您的帮助将不胜感激。 如果我提供了足够的信息,则表示歉意。让我知道,我将更新问题。

    n = 0
    q = int(input("enter a number"))
    s = range(1,q,2)
    r = s
    while r <= q:
    n= n+1
    s= s+n
    print("smallest N is",n)

答案

    q = int(input("enter a number "))
    s=[1]   #start from 1
    while sum(s) < q: #check if sum of s is less than input
    s.append(s[-1]+2) #s[-1] will get the last element of list and +2 will ensure odd numbers are inserted in list
    print("smallest N is",s[-1]) #get the last element in the list

完美运行 感谢大家的所有回答,非常感谢。 我只是好奇。如果我有

Enter a positive integer: 25
8 is the largest k such that 0+2+4+6+...+k < 25

应该得到8但得到10 `

q = int(input("enter a number "))
s=[0]   #start from 0
while sum(s) < q: 
s.append(s[-1]+2) 
print("smallest N is",s[-1])

`

解决方法

这是一个简单的例子。

q = int(input("enter a number "))
s=[1]   #start from 1
while sum(s) < q: #check if sum of s is less than input
    s.append(s[-1]+2) #s[-1] will get the last element of list and +2 will ensure odd numbers are inserted in list
print("smallest N is",s[-1]) #get the last element in the list
,

我提出了这种解决方案:

q = int(input("enter a number"))
s = 0
r = iter(range(1,q,2))
while not s >= q:
    n = next(r)
    s += n
print("smallest N is",n)

它会在以下几点上修改您的版本:

  1. 我使用s作为累积金额,并用0初始化
  2. 我将r设为迭代器,当我在其上运行next()时会给我下一个值
  3. 我修改了while循环的条件,直到最终总和大于或等于用户输入的数字s为止,直到总和q为止。
  4. 在循环中,我检索范围迭代器的下一个元素,将其保存到n并将其添加到总和s中。因此,我记得在最终总和s大于或等于q
  5. 之前添加的最后一项
,

range功能非常强大,但是您使用的方式不正确(这里确实没有必要)。

positive_integer = int(input('Enter a positive integer: '))
assert positive_integer >= 1
n = 1
sum = 1
while sum < positive_integer:
    n += 2
    sum += n
print('Smallest n is ',n)
,

您的代码出错,因为无法确定整数是否等于或小于范围。例如,我无法确定5是否小于,等于或大于60到110之间的数字。这不是数学的工作原理。但是,您可以确定该数字是小于,等于还是大于该范围内的最小数字。我建议您为此更改代码:

while r <= min(1,2):
    n += 1
    s += n
print("Smallest N is " + n)
    

,
q = int(input("enter a number"))
sum = 0;
n=0;
for i in range(1,2):
    sum = sum+i
    if(sum >= q):
        n=i
        break

print("smallest N is",n)

可以做到

,

一个示例可以使您更容易理解: 假设q = 5 然后s = range(1,5,2)(在s上循环将生成1,3) 因此,s不是1的特定数字,而是“多个”数字。设置r = s表示r是多个数字。检查r

应该这样做。我尝试对它进行一些编码,以便解释,而又不做太多改动。

q = int(input("enter a number"))
r = range(1,2)
s = 0
n = 0
for i in r:
    n = n + 1
    s = s + i
    if s >= q:
        break
print("smallest N is",i)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?