You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python统计数字序列中相邻数对前者可被后者整除的数量

Python 相邻数对整除计数实现

功能说明

  • 支持用户自定义输入数字序列长度
  • 引导用户依次录入序列内的每个数字
  • 自动统计相邻数对中第一个数可被第二个数整除的总对数并输出结果

可运行代码

# 获取序列长度
seq_length = int(input("Enter the length of the sequence: "))
num_list = []
# 依次收集用户输入的数字
for i in range(seq_length):
    input_num = int(input(f"Enter number {i+1}: "))
    num_list.append(input_num)
count = 0
# 遍历统计符合要求的相邻数对
for idx in range(len(num_list)-1):
    if num_list[idx] % num_list[idx+1] == 0:
        count += 1
# 输出结果
print(f"The number of pairs of adjacent where 1st divisible by 2nd  is :{count}")

效果验证

按照示例输入参数:

  • 序列长度输入5
  • 依次输入75、25、10、30、3
    最终输出结果为2,和预期示例完全匹配。如果需要中文交互界面,替换input和print内的文本内容即可。

内容的提问来源于stack exchange,提问作者Kecha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 22:45:06