方法一:用空间的办法去换时间。开辟一个新的空间
#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
void search1(
int a[],
int len)
{
int sp[
1000] = {
0 };
int i =
0;
int index =
0;
int max =
0;
for (
int i =
0; i < len; i++)
{
index = a[i] -
1;
sp[index]++;
}
for (
int i =
0; i <
1000; i++)
{
if (max < sp[i])
{
max = sp[i];
}
}
for (
int i =
0; i <
1000;i++)
{
if (max == sp[i])
{
cout << i +
1 << endl;
}
}
}
int main(){
int array[
100] = {
1,
3,
3,
3,
6,
6,
6,
6,
6,
6,
6,
6,
7,
7,
7,
8,
8 };
search1(
array,
sizeof(
array)/
sizeof(*
array));
system(
"pause");
return 0;
}
方法二:排序的方法找到数字最多的数
#include<iostream>
using namespace std;
int main(){
int array[] = {
1,
3,
3,
3,
6,
6,
6,
6,
6,
6,
6,
6,
7,
7,
7,
8,
8 };
int temp =
0;
int max =
0;
int num =
0;
int N =
sizeof(
array) /
sizeof(*
array);
for (
int i =
0; i < N; i++)
{
for (
int j = i; j < N; j++)
{
if (
array[i] ==
array[j])
{
temp++;
}
}
if (max < temp)
{
max = temp;
num =
array[i];
}
temp =
0;
}
cout << num<< endl;
return 0;
}