翻译好的部分:颠倒对方

huangapple go评论83阅读模式
英文:

Flipping The Pairs

问题

以下是翻译好的部分:

程序要求输入一个数字,然后打印出从1到该数字的所有正整数值。但是,数字的顺序会改变,使得每对数字都被翻转。也就是说,2出现在1之前,4出现在3之前,依此类推。例如:

  1. 请输入一个数字:6
  2. 2
  3. 1
  4. 4
  5. 3
  6. 6
  7. 5
  1. num = int(input("请输入一个数字:"))
  2. r = 2
  3. t = 1
  4. while True:
  5. if t >= num:
  6. break
  7. print(r)
  8. print(t)
  9. r += 2
  10. t += 2

这段代码对偶数运行良好,但在处理奇数时会出现问题。例如,对于输入5,结果应该是:

  1. 请输入一个数字:5
  2. 2
  3. 1
  4. 4
  5. 3
  6. 5

但实际上我的输出是:

  1. 请输入一个数字:5
  2. 2
  3. 1
  4. 4
  5. 3

希望这可以帮助你。

英文:

The program asks for a number and then prints out all the positive integer values from 1 up to the number. However, the order of the numbers is changed so that each pair or numbers is flipped. That is, 2 comes before 1, 4 before 3 and so forth. For example:

  1. Please type in a number: 6
  2. 2
  3. 1
  4. 4
  5. 3
  6. 6
  7. 5
  1. num = int(input("Please type in a number: "))
  2. r = 2
  3. t = 1
  4. while True:
  5. if t >= num:
  6. break
  7. print(r)
  8. print(t)
  9. r += 2
  10. t += 2

The code works fine with even numbers but then struggles with odd numbers. For example with the input 5 the result should be:

  1. Please type in a number: 5
  2. 2
  3. 1
  4. 4
  5. 3
  6. 5

But instead my output comes out as:

  1. Please type in a number: 5
  2. 2
  3. 1
  4. 4
  5. 3

Any help would be appreciated.

答案1

得分: 1

num为奇数且t == num时,例如当num5t变为5时,循环会在第3次执行之前简单地退出。如果期望的输出是输出5而不是6(因为没有什么需要“翻转”的),你可以在num为奇数时简单地执行print(num)。然而,这与num=6的情况不一致,因为第五次输出不同,所以我建议你仔细检查一下。

英文:

Looking at your own example, when num is odd and t == num, for example when num is 5 and t becomes 5, then the loop simply exits without executing for the 3rd time. If the expected output is to output 5 instead of 6 (because there is nothing to "flip"), you can simply do print(num) if num is odd. However this is inconsistent with the num=6 case as the fifth output is different, so I would recommend you double-check this.

答案2

得分: 0

这里是翻译好的部分:

也许会有几种解决方案,但这里有一个简单的修复方法:

  1. num = 5 # 用于简单测试的赋值
  2. r = 2
  3. t = 1
  4. while t <= num:
  5. if t != num: # 如果t == num,则跳过下面的行
  6. print(r)
  7. print(t)
  8. r += 2
  9. t += 2

一行代码的示例:

  1. print(sorted(range(1, num+1), key=lambda x: x + 2 * (x % 2)))

使用列表推导式:

  1. def fn(x):
  2. return x + 2 * (x % 2) - 1
  3. print([x if x==num and num%2 else fn(x) for x in range(1, num+1)])

以上示例的更可读版本:

  1. output = [x + 2 * (x % 2) - 1 for x in range(1, num+1)]
  2. if num % 2:
  3. output[-1] = num
  4. print(output)
英文:

Maybe there will be several solutions, but a simple fix is here:

  1. num = 5 # assigned for simple testing
  2. r = 2
  3. t = 1
  4. while t &lt;= num:
  5. if t != num: # if t == num, skip the below line
  6. print(r)
  7. print(t)
  8. r += 2
  9. t += 2

An one-liner example:

  1. print(sorted(range(1, num+1), key=lambda x: x + 2 * (x % 2)))

With list comprehension:

  1. def fn(x):
  2. return x + 2 * (x % 2) - 1
  3. print([x if x==num and num%2 else fn(x) for x in range(1, num+1)])

More readable version of the above example:

  1. output = [x + 2 * (x % 2) - 1 for x in range(1, num+1)]
  2. if num % 2:
  3. output[-1] = num
  4. print(output)

答案3

得分: 0

你只能在r &lt;= num时才能打印r,并在其后执行t &gt; num的检查:

  1. num = int(input("请输入一个数字:"))
  2. r = 2
  3. t = 1
  4. while True:
  5. if r <= num:
  6. print(r)
  7. if t > num:
  8. break
  9. print(t)
  10. r += 2
  11. t += 2
英文:

You can print(r) only when r &lt;= num, and move the t &gt; num check after it:

  1. num = int(input(&quot;Please type in a number: &quot;))
  2. r = 2
  3. t = 1
  4. while True:
  5. if r &lt;= num:
  6. print(r)
  7. if t &gt; num:
  8. break
  9. print(t)
  10. r += 2
  11. t += 2

答案4

得分: 0

你可以对你的现有代码进行简单更改,如下所示:

  1. num = int(input("请输入一个数字:"))
  2. r = 2
  3. t = 1
  4. while True:
  5. if t >= num:
  6. if num % 2 != 0:
  7. print(r - 1) # 显示最后一个奇数
  8. break
  9. print(r)
  10. print(t)
  11. r += 2
  12. t += 2

输出将如所期望地显示。

英文:

you can do a simple change to your exiting code like below

  1. num = int(input(&quot;Please type in a number: &quot;))
  2. r = 2
  3. t = 1
  4. while True:
  5. if t &gt;= num:
  6. if num % 2 != 0:
  7. print(r-1) # Display the last odd number
  8. break
  9. print(r)
  10. print(t)
  11. r += 2
  12. t += 2

The output will come as desired

huangapple
  • 本文由 发表于 2023年6月9日 13:46:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定