读取整数序列后用列表 count 统计指定数字的出现次数。
OJ: noi_openjudge
题目 ID: ch0106-01
难度:入门
标签:数组计数python
日期: 2026-07-30 23:01
题意
统计长度为
思路
读入序列后,列表的 count(target) 直接返回目标值出现次数。
代码
Python代码
python
length = int(input())
numbers = list(map(int, input().split()))
target = int(input())
print(numbers.count(target))C++代码
cpp
#include <cstdio>
int main(){
int a[101];
int n,m;
int i;
scanf("%d",&n);
for (i=1;i<=n;i++){
scanf("%d",&a[i]);
}
scanf("%d",&m);
int cnt =0;
for (i=1;i<=n;i++){
if( a[i] == m)
cnt++;
}
printf("%d",cnt);
return 0;
}复杂度
时间复杂度为
总结
Python 列表的 count 适合小规模的单值频次统计。