【问题标题】:Looped nesting -- Factorising [duplicate]循环嵌套-分解[重复]
【发布时间】:2015-09-21 09:03:08
【问题描述】:

我正在尝试编写一个程序,该程序接受用户输入、限制并打印一个表格,以可视化从 1 到限制的每个整数的所有因子。每行表示 1 和 limit 之间的整数,而第一行表示数字 1,第二行表示数字 2 等。对于第 n 行中的给定位置 i(从 1 开始),如果 i 是限制的一个因素,则有一个'*',如果没有,那么'-'。

例如对于 20 的输入,它应该产生:

Maximum number to factorise: 20
* - - - - - - - - - - - - - - - - - - - 
* * - - - - - - - - - - - - - - - - - - 
* - * - - - - - - - - - - - - - - - - - 
* * - * - - - - - - - - - - - - - - - - 
* - - - * - - - - - - - - - - - - - - - 
* * * - - * - - - - - - - - - - - - - - 
* - - - - - * - - - - - - - - - - - - - 
* * - * - - - * - - - - - - - - - - - - 
* - * - - - - - * - - - - - - - - - - - 
* * - - * - - - - * - - - - - - - - - - 
* - - - - - - - - - * - - - - - - - - - 
* * * * - * - - - - - * - - - - - - - - 
* - - - - - - - - - - - * - - - - - - - 
* * - - - - * - - - - - - * - - - - - - 
* - * - * - - - - - - - - - * - - - - - 
* * - * - - - * - - - - - - - * - - - - 
* - - - - - - - - - - - - - - - * - - - 
* * * - - * - - * - - - - - - - - * - - 
* - - - - - - - - - - - - - - - - - * - 
* * - * * - - - - * - - - - - - - - - * 

我目前有:

limit = input('Maximum number to factorise: ')
for i in range(1,int(limit)):
    line = '{:2}:'.format(i)
    for j in range (1,11):
        line += "{:4}".format(i % j)
    print(line)

但它给出了:

Maximum number to factorise: 20
 1:   0   1   1   1   1   1   1   1   1   1
 2:   0   0   2   2   2   2   2   2   2   2
 3:   0   1   0   3   3   3   3   3   3   3
 4:   0   0   1   0   4   4   4   4   4   4
 5:   0   1   2   1   0   5   5   5   5   5
 6:   0   0   0   2   1   0   6   6   6   6
 7:   0   1   1   3   2   1   0   7   7   7
 8:   0   0   2   0   3   2   1   0   8   8
 9:   0   1   0   1   4   3   2   1   0   9
10:   0   0   1   2   0   4   3   2   1   0
11:   0   1   2   3   1   5   4   3   2   1
12:   0   0   0   0   2   0   5   4   3   2
13:   0   1   1   1   3   1   6   5   4   3
14:   0   0   2   2   4   2   0   6   5   4
15:   0   1   0   3   0   3   1   7   6   5
16:   0   0   1   0   1   4   2   0   7   6
17:   0   1   2   1   2   5   3   1   8   7
18:   0   0   0   2   3   0   4   2   0   8
19:   0   1   1   3   4   1   5   3   1   9

所以我有正方形,但是如何用“*”替换 0,用“-”替换其他所有内容?以及如何让我的程序按要求执行?

【问题讨论】:

  • line += "{:4}".format('-' if (i % j) else '*').
  • 所有打印的行都有 lenth 11(不管你输入了什么),因为你在for j in range (1,11): 行中设置了行 lentgh 11

标签: python python-3.x


【解决方案1】:

您也应该在内部循环中使用变量ilimit。所以你会生成一个limit * limit 输出。此外,您必须迭代直到 limit + 1 才能获得完整的 limit 行。

详细版本:

# get a maximim number and cast it to int
limit = int(input('Maximum number to factorise: '))

# prints a header [1; limit]
print " " + "".join(["%3d" % i for i in xrange(1, limit + 1)])
# outer loop: [1; limit]
for i in range(1, limit + 1):
    line = '%2d ' % i  # format string row number
    # inner loop: [1; limit]!
    for j in range (1, i + 1):  # you can also set limit + 1
        if i % j:  # if i % != 0
            line += '-  '
        else:
            line += '*  '
    print(line)

生成:

   1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
 1 *
 2 *  *
 3 *  -  *
 4 *  *  -  *
 5 *  -  -  -  *
 6 *  *  *  -  -  *
 7 *  -  -  -  -  -  *
 8 *  *  -  *  -  -  -  *
 9 *  -  *  -  -  -  -  -  *
10 *  *  -  -  *  -  -  -  -  *
11 *  -  -  -  -  -  -  -  -  -  *
12 *  *  *  *  -  *  -  -  -  -  -  *
13 *  -  -  -  -  -  -  -  -  -  -  -  *
14 *  *  -  -  -  -  *  -  -  -  -  -  -  *
15 *  -  *  -  *  -  -  -  -  -  -  -  -  -  *
16 *  *  -  *  -  -  -  *  -  -  -  -  -  -  -  *
17 *  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  *
18 *  *  *  -  -  *  -  -  *  -  -  -  -  -  -  -  -  *
19 *  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  *
20 *  *  -  *  *  -  -  -  -  *  -  -  -  -  -  -  -  -  -  *

【讨论】:

    【解决方案2】:

    你想要这样的东西:

    limit = input('Maximum number to factorise: ')
    for i in range(1,int(limit)):
        line = '{:2}:'.format(i)
        for j in range (1,11):
            if i % j == 0:
                line += "{:4}".format('0')
            else:
                line += "{:4}".format('-')
        print(line)
    

    【讨论】:

    • 如何删除生成输出旁边的数字列表? (例如:1:、2:、3: 等)
    • @Curious 将 line = '{:2}:'.format(i) 更改为 line = ''
    猜你喜欢
    • 1970-01-01
    • 2017-09-30
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多