翻译好的部分:颠倒对方

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

Flipping The Pairs

问题

以下是翻译好的部分:

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

请输入一个数字:6
2
1
4
3
6
5
num = int(input("请输入一个数字:"))
r = 2
t = 1

while True:
    if t >= num:
        break
    print(r)
    print(t)
    r += 2
    t += 2

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

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

但实际上我的输出是:

请输入一个数字:5
2
1
4
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:

Please type in a number: 6
2
1
4
3
6
5
num = int(input("Please type in a number: "))
r = 2
t = 1


while True:
    if t >= num:
        break
    print(r)
    print(t)
    r += 2
    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:

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

But instead my output comes out as:

Please type in a number: 5
2
1
4
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

这里是翻译好的部分:

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

num = 5 # 用于简单测试的赋值
r = 2
t = 1

while t <= num:
    if t != num:  # 如果t == num,则跳过下面的行
        print(r)
    print(t)
    r += 2
    t += 2

一行代码的示例:

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

使用列表推导式:

def fn(x):
    return x + 2 * (x % 2) - 1

print([x if x==num and num%2 else fn(x) for x in range(1, num+1)])

以上示例的更可读版本:

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

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

num = 5 # assigned for simple testing
r = 2
t = 1


while t &lt;= num:
    if t != num:  # if t == num, skip the below line
        print(r)
    print(t)
    r += 2
    t += 2

An one-liner example:

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

With list comprehension:

def fn(x):
    return x + 2 * (x % 2) - 1

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:

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

答案3

得分: 0

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

num = int(input("请输入一个数字:"))
r = 2
t = 1

while True:
    if r <= num:
        print(r)
    if t > num:
        break
    print(t)
    r += 2
    t += 2
英文:

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

num = int(input(&quot;Please type in a number: &quot;))
r = 2
t = 1

while True:
    if r &lt;= num:
        print(r)
    if t &gt; num:
        break
    print(t)
    r += 2
    t += 2

答案4

得分: 0

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

num = int(input("请输入一个数字:"))
r = 2
t = 1

while True:
    if t >= num:
        if num % 2 != 0:
            print(r - 1)  # 显示最后一个奇数
        break
    print(r)
    print(t)
    r += 2
    t += 2

输出将如所期望地显示。

英文:

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

num = int(input(&quot;Please type in a number: &quot;))
r = 2
t = 1

while True:
    if t &gt;= num:
        if num % 2 != 0:
            print(r-1)  # Display the last odd number
        break
    print(r)
    print(t)
    r += 2
    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:

确定