# My coding way (10)

1452: 众数问题

1行多重集S中元素个数n(n<=50000)；接下来的n 行中，每行有一个自然数。

6

1

2

2

2

3

5

2

3

>_< 没什么好想法…

#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

typedef struct
{
int value;
int times;
}pack;

pack number[50000];
int number_max=0;

int tmp;

int main()
{
int times;
scanf("%d",×);
for(int i=0;i<times;i++)
{
scanf("%d",&tmp);
bool finded=false;
for(int searching=0;searching<number_max;searching++)
{
if(tmp==number[searching].value)
{
number[searching].times++;
finded=true;
break;
}
}
if(finded)
{
continue;
}
else
{
number[number_max].value=tmp;
number_max++;
}
}
int target_value=number[0].value;
int target_times=number[0].times;
for(int i=1;i<number_max;i++)
{
if(target_times<number[i].times)
{
target_times=number[i].times;
target_value=number[i].value;
}
else if(target_times==number[i].times)
{
if(target_value>number[i].value)
{
target_value=number[i].value;
}
}
}
printf("%d\n%d\n",target_value,target_times+1);
return 0;
}

#### My coding way (6)

2015-10-23 19:43:50

#### My coding way (4)

2015-10-23 13:16:59

#### My coding way (9)

2015-11-07 20:57:13

#### My coding way (2)

2015-10-21 23:46:07

#### My coding way (1)

2015-10-21 23:41:18

#### My coding way (7)

2015-10-23 19:46:19

#### My coding way (8)

2015-11-07 20:55:08

#### My coding way (5)

2015-10-23 13:19:25

#### My coding way (3)

2015-10-23 13:13:21

#### Programming Interviews Exposed(4th) 无水印原版pdf

2018年04月25日 15.72MB 下载