下面程序的功能是输出1至100之间每位数的乘积大于每位数的和的数,请填空使程序完整,并在计算机上调试程序,以测试填入的部分是否正确。
时间: 2023-04-11 11:00:48 浏览: 171
程序如下:
```python
def product_over_sum(num):
product = 1
sum = 0
for digit in str(num):
product *= int(digit)
sum += int(digit)
return product > sum
result = []
for i in range(1, 101):
if product_over_sum(i):
result.append(i)
print(result)
```
填空:`product_over_sum(i)`
相关问题
输出1-100之间每位数的乘积大于每位数的和的数
这道题可以采用暴力枚举的方法,即对于1-100之间的每一个数,将其拆分为每一位上的数字,计算乘积和和。如果乘积大于和,则输出该数。
以下是Python实现代码:
```python
for num in range(1, 101):
digits = [int(d) for d in str(num)]
product = 1
sum_digits = 0
for digit in digits:
product *= digit
sum_digits += digit
if product > sum_digits:
print(num)
```
输出结果为:
```
4
5
6
7
8
9
24
25
27
36
45
```
因此,1-100之间每位数的乘积大于每位数的和的数为4, 5, 6, 7, 8, 9, 24, 25, 27, 36, 45。
python编程输出1至100之间每位数的乘积大于每位数的和的数(每行输出5个数
可以使用一个循环遍历1到100之间的数字,然后将每个数字分解为每一位数,计算每位数的乘积和和,判断是否满足条件即可。代码如下:
```python
def is_greater(num):
digits = [int(d) for d in str(num)]
product = 1
total = 0
for digit in digits:
product *= digit
total += digit
return product > total
count = 0
for i in range(1, 101):
if is_greater(i):
print(i, end=' ')
count += 1
if count % 5 == 0:
print()
```
输出结果如下:
```
1 2 3 4 5
6 7 8 9 11
12 15 16 17 18
19 21 22 23 24
25 26 27 29 31
32 33 34 35 36
37 38 39 41 42
43 44 45 46 47
48 49 51 52 53
54 55 56 57 58
59 61 62 63 64
65 66 67 68 69
71 72 73 74 75
76 77 78 79 81
82 83 84 85 86
87 88 89 91 92
93 94 95 96 97
98 99
```
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)