【问题标题】:printing logical sequence in python在python中打印逻辑序列
【发布时间】:2020-10-02 12:18:35
【问题描述】:

我的程序读取两个数字 X 和 Y (X

我的代码:

x, y = map(int, input().split())
count = 0
a, b, c = 1, 2, 3

while count < (y//x):
    print('{} {} {}'.format(a, b, c))
    a += d
    b += d
    c += d
    count += 1

示例输入:

3 99

我想要的输出:每个序列必须打印在一行中,每个数字之间有一个空格,如下例

1 2 3
4 5 6
7 8 9
10 11 12
.....
....
97 98 99

【问题讨论】:

    标签: python-3.x while-loop conditional-statements sequence


    【解决方案1】:

    你必须使用两个循环,代码如下所示:

    x, y = map(int, input().split())
    count = 0
    
    while count < (y//x):
        start = count*x + 1
        end = (count + 1) * x + 1
        for i in range(start,end,1):
            print(i,end=" ")
        print("")
        count+=1
    

    【讨论】:

    • 这是一个在线法官问题@MohammadShakib?
    • 是的Problem link
    • 那你自己解决吧。我可以帮助您理解为什么在线法官会做出“陈述错误”的判决。 “Presentation Error”意味着您在解决方案中打印了一些额外的空间或新行。现在试着自己找出问题并解决它。
    【解决方案2】:

    您可以使用 range 生成 y 数字列表,然后您可以在 x 块中对其进行迭代

    def print_lines(x, y):
       nums = list(range(y))
       for i in nums[::x]:
          print(" ".join([str(num + 1) for num in nums[i:i + x]]))
    
    print_lines(3, 99)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      • 2012-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多