kinkon 发表于 2020-3-31 22:12:02

def f364(target):
    target = abs(target)
    n = 0
    for i in range(1, target+2):
      n += i
      if n == target:
            return i
      elif n > target and (n-target)%2==0:
            return i

蒋博文 发表于 2020-3-31 23:26:21

先占个楼

sYMMetrY 发表于 2020-4-1 01:43:58

#使用递归的思路解题,不知道速度会不会拖得太慢
def f364(num, list1 = , count = 1):
    if num in list1:
      return count
    else:
      list2=[]
      count += 1
      for each in list1:
            list2.append(each + count)
            list2.append(each - count)

      return f364(num, list2, count)


zltzlt 发表于 2020-4-1 13:23:42

TJBEST 发表于 2020-3-31 17:35
我昨天的363题做出来了,我写在这里也给测一下吧
旧 363题

通过了,52 ms

你在原 363 题回复一下

zltzlt 发表于 2020-4-1 13:25:26

chen971130 发表于 2020-3-31 18:51
# 思路:先一直走到target之前或之后,(或直接到target),
# 然后分别反复移动到target(每移动一格需 ...

解答错误

输入:-1
输出:-3
预期结果:1

zltzlt 发表于 2020-4-1 13:26:18

Herry2020 发表于 2020-3-31 20:07
def fun364(target):
    sum = 0
    n = 1


解答错误

输入:-1
输出:6
预期结果:1

zltzlt 发表于 2020-4-1 13:26:49

ouyunfu 发表于 2020-3-31 20:37


128 ms

zltzlt 发表于 2020-4-1 13:27:25

sunrise085 发表于 2020-3-31 21:05
我把下午写的修改了一下,while循环最多需要两次

44 ms

zltzlt 发表于 2020-4-1 13:27:48

archlzy 发表于 2020-3-31 21:26


targert ?

zltzlt 发表于 2020-4-1 13:28:16

kinkon 发表于 2020-3-31 22:12


204 ms

风魔孤行者 发表于 2020-4-1 13:57:22

def f(n):
    x = 0
    while x*(x+1)/2 < n or (x*(x+1)/2-n)%2 or (x*(x+1)/2-n)/2 == 2:
      x += 1
    return x

chen971130 发表于 2020-4-1 16:19:44

zltzlt 发表于 2020-4-1 13:25
解答错误

输入:-1


def min_step(target):
    target = abs(target)
    count = 1
    sum1 = 0
    while sum1 < target:
      sum1 = sum1 + count
      count += 1
    if sum1 == target:
      print(count - 1)
    else:
      number1 = (target-sum1+count-1)*2 + count - 2
      number2 = (sum1-target)*2 + count - 1
      print(min())
min_step(3)

那就再加个绝对值就好啦

蒋博文 发表于 2020-4-1 16:45:05

def fun364(target):
      t = abs(target)
      k = 0
      s = 0
      while s < t:
            k += 1
            s += k
      dt = s - t
      if dt % 2 == 0:
            return k
      else:
            if k % 2 == 0:
                return k + 1
            else:
                return k + 2
363题实在是看不懂题目,只好来做364

zltzlt 发表于 2020-4-1 17:46:30

sYMMetrY 发表于 2020-4-1 01:43


解答错误

输入:-1
输出:2
预期结果:1

zltzlt 发表于 2020-4-1 17:48:29

风魔孤行者 发表于 2020-4-1 13:57


解答错误

输入:2
输出:4
预期结果:3

zltzlt 发表于 2020-4-1 17:53:04

chen971130 发表于 2020-4-1 16:19
那就再加个绝对值就好啦

解答错误

输入:4
输出:4
预期结果:3

zltzlt 发表于 2020-4-1 18:59:40

蒋博文 发表于 2020-4-1 16:45
363题实在是看不懂题目,只好来做364

132 ms

风魔孤行者 发表于 2020-4-1 19:40:47

本帖最后由 风魔孤行者 于 2020-4-1 19:44 编辑

zltzlt 发表于 2020-4-1 17:48
解答错误

输入:2


def f(n):
    n = abs(n)
    x = 0
    while x*(x+1)/2 < n or (x*(x+1)/2-n)%2:
      x += 1
    return x

sYMMetrY 发表于 2020-4-1 20:37:47

zltzlt 发表于 2020-4-1 17:46
解答错误

输入:-1


题目中,这句话<第 n 次移动(从 1 开始)可以走 n 步。>意思理解有偏差,理解为:每次第一步都先移动到1的意思了。只需将代码中函数内list1的初始值改为[-1,1]即可。
def f364(num, list1 = [-1,1], count = 1):
    if num in list1:
      return count
    else:
      list2=[]
      count += 1
      for each in list1:
            list2.append(each + count)
            list2.append(each - count)

      return f364(num, list2, count)

快马加鞭未下鞍 发表于 2020-4-2 18:02:24

sunrise085 发表于 2020-3-31 17:53


这个为什么可以这样算啊,真的神奇
页: 1 [2] 3
查看完整版本: Python:每日一题 364