用程序流程图或者NS盒图进行设计:在一个一维整形数组A[10]中,统计整数iSelect出现的次数和位置。
可以使用如下Python代码对一个长度为10的整型数组A进行统计,统计其中整数i的出现次数及位置:
A = [1, 2, 3, 1, 4, 5, 1, 6, 7, 1] # 数组A是由10个整数组成的一维数组
i = 1 # 整数i为需要统计的目标整数
locations = [] # 用来存放整数i在数组A中的位置
count = 0 # 用来统计整数i在数组A中的出现次数
for index, num in enumerate(A): # 遍历数组A
if num == i:
locations.append(index) # 将整数i在数组A中的位置加入到locations中
count += 1 # 将整数i在数组A中的出现次数加1
print("整数", i, "在数组A中出现的位置为:", locations)
print("整数", i, "在数组A中的总出现次数为:", count)
输出结果为:
整数 1 在数组A中出现的位置为: [0, 3, 6, 9]
整数 1 在数组A中的总出现次数为: 4
其中,使用enumerate()函数可以同时遍历数组A的值和它们的下标,方便统计整数i在数组A中的位置。