环状嵌套 - 分解

时间:2015-09-21 09:03:09

标签: python python-3.x

我正在尝试编写一个程序,该程序接受用户输入,限制并打印一个表,以显示从1到限制的每个整数的所有因子。每行代表1和极限之间的整数,而第一行代表数字1,第二行代表2等。对于给定位置i(从1开始)在行n中,如果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,用“ - ”替换其他所有东西?我如何让我的程序按照要求去做?

2 个答案:

答案 0 :(得分: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 *  *  -  *  *  -  -  -  -  *  -  -  -  -  -  -  -  -  -  *

答案 1 :(得分:0)

你想要这样的东西:

var whereExp = cars.AsQueryable<Car>().Where(whereConditionSub).Expression;
var countMethod = new Func<IQueryable<Car>, int>(Queryable.Count).Method;
var eSub1 = Expression.Call(countMethod, whereExp);