可视化因素 [英] Visualising factors

查看:60
本文介绍了可视化因素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,该问题要求我打印一个表格来可视化每个整数的所有因子,范围从 1limit.然后它指定给定位置i,从1开始在一行n,然后一个*表示in 的因数,- 表示不是.下面是一个输出示例.

I've come across a problem that asks me to print a table to visualize all factors of each integer ranging from 1 to limit. Then it specifies that a given position i, starting from 1 in a row n, then a * indicates that i is a factor of n, and - indicates that it is not. Below is an example of the output.

我知道我必须使用模数运算符来测试因子和 for 循环,但我对构建代码感到很困惑.

I know that I've to make use modulus operator to test the factors and a for loop, but I'm so confused with constructing the code.

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

推荐答案

我不会给出完整的代码,因为这显然是一个家庭作业.

I am not going to give the complete code, since this is clearly an homework assignment.

但是这里有一个伪算法可以帮助您入门 -

But here is a pseudo algorithm that can get you started -

  1. 你需要两个 for 循环,一个嵌套在另一个里面.第一个 for 循环用于遍历行,第二个内部 for 循环用于遍历列.

  1. You would need two for loops, one nested inside the other. The first for loop for going over the rows, second inner for loop for going over the columns.

在嵌套的 for 循环中,您需要检查内部 for 循环的计数器是否可以被外部 for 循环的计数器变量整除,如果是则需要打印 * 不换行(为此,您可以在 Python 3 中使用 end='' 参数,或者在要在 Python 2 中打印的内容后使用 ,),如果不可整除打印-.

Inside the nested for loop, you would need to check whether the counter for inner for loop is divisible by the counter variable of the outer for loop, if it is you need to print * without giving a new line (To do this you can use end='' argument in Python 3 , or use , after what you want to print in Python 2), if not divisible print -.

最后在完成内循环后,您需要打印另一个换行符,以便下一行从下一行开始.

Finally after completing the inner loop, you would need to print another newline, so that the next row starts at next line.

这篇关于可视化因素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆