读入序列后分别统计 1、5、10 的出现次数。
OJ: noi_openjudge
题目 ID: ch0105-11
难度:入门
标签:统计python
日期: 2026-07-30 23:01
题意
给定
思路
读成列表后,numbers.count(value) 返回指定值出现次数。只统计三个固定目标,分别调用 count 很直观。
代码
Python代码
python
count = int(input())
numbers = list(map(int, input().split()))
print(numbers.count(1))
print(numbers.count(5))
print(numbers.count(10))C++代码
cpp
#include <cstdio>
int main(){
int i;
int n;
scanf("%d",&n);
int a=0,b=0,c=0;
int t;
for (i=1;i<=n;i++){
scanf("%d",&t);
if( t == 1)
a++;
if( t == 5)
b++;
if( t == 10)
c++;
}
printf("%d\n",a);
printf("%d\n",b);
printf("%d\n",c);
return 0;
}复杂度
每次 count 扫描序列,三次统计的时间复杂度仍为
总结
目标值数量很少时,列表的 count 是清晰的直接解法。