戳这看系统原题
4068: 数组元素的查找新
时间限制: 1.000 Sec 内存限制: 128 MB
题目描述
给你m个整数,查找其中有无值为x的数,输出x一共有几个
输入
第一行一个整数m:数的个数 ( 0 ≤ m ≤ 100 )
第二行m个整数(空格隔开)( 这些数在 0-999999范围内 )
第三行为要查找的数x
输出
一个整数,表示x的个数。
样例
输入
7
10 21 3 3 5 3 7
3
输出
3
戳这提交程序
解题思路 #1:
这道题难度不大,可以在主函数里循环列举比较,得到结果。
作者采用函数,在函数中比较,使程序简洁明了。
主函数中很清晰,输入+输出。
函数中循环比较,最终返回结果就OK啦!
C++程序 #1:
#include<bits/stdc++.h>
using namespace std;
int m,a[105],x,i;
int find(int k)//查找所要求找的数的个数。
{
int tot=0;
for(int j=1;j<=m;j++)
{
if(a[j]==k)//枚举比较。
{
tot++;
}
}
return tot;//返回个数。
}
int main()
{
cin>>m;
for(i=1;i<=m;i++)
{
cin>>a[i];
}
cin>>x;
cout<<find(x);
return 0;
}
解题思路 #2:
主函数中,先输入,后进行枚举比较。
用tot变量计个数,最后输出。
C++程序:
#include<bits/stdc++.h>
using namespace std;
int m,a[105],i,x,tot;
int main()
{
cin>>m;
for(i=1;i<=m;i++)
{
cin>>a[i];
}
cin>>x;//以上为输入
for(i=1;i<=m;i++)
{
if(a[i]==x)//比较
tot++;//计个数
}
cout<<tot;//输出
return 0;
}
好了,最后跪求三连
(哪怕关注一下也行,系不系很银杏?)