#include
#include
#include
using namespace std;
void frequency(vector nums) {
int n = nums.size();
int count = 0;
int target = nums[0];
for (int i = 0; i < n; i++) {
if (nums[i] == target)
count++;
}
cout << "第一个输入数字出现的频率为:" << count << endl;
}
int main() {
vector nums = { 1,2,3,4,5,6,1,3,5,7,9,1,4,5 };
frequency(nums);
return 0;
}
本程序接受一个整数向量nums,它包含要分析的数字。函数计算第一个数在向量中出现的次数,并将该次数作为输出打印到屏幕上。在主函数中,我们定义了一个整数向量nums,其中包含了一些数字供我们测试。最后我们通过调用函数frequency来得到第一个数字的频率。
上一篇:编写一个函数reverse_substrings,函数输入一个字符串,对其中的所有子字符串进行反转并返回结果。
下一篇:编写一个函数sum_integers(start, end),该函数接受两个正整数start和end作为参数,并返回所有整数的和。