360笔试第二题

360第二道题:放彩球。写的有些繁琐,有什么好的算法吗?
#include <iostream>

using namespace std;

int main()
{
int a[10000] = { 0 };
int n;
int i, j;
int min = 10000;
int count = 0;
cin >> n;

for (i = 0; i < n; i++)
{
cin >> j;
a[j]++;
}

bool flag = true;
for (i = 0; i < 10000; i++)
{
if (a[i] > 0)
{
if (a[i] == 1)
{
flag = false;
break;
}
else
{
if (min > a[i])
{
min = a[i];
}
}
}
}


for (i = 0; i < 10000; i++)
{
if (a[i] > 0)
{
if (a[i] % min != 0)
{
flag = false;
break;
}
else
{
count += a[i] / min;
}
}
}

if (flag)
{
cout << count << endl;
}
else
{
cout << 0 << endl;
}

return 0;
}

#360公司##笔试题目#
全部评论
python 15行 def main(): N = int(input()) nums = [int(i) for i in input().split()] s = set(nums) d = {i:nums.count(i) for i in s} m = min(d.values()) if m<2: return 0 res = 0 for k in d: cnt = d[k]//m if d[k]%m!=0: return 0 res += cnt return res
点赞 回复
分享
发布于 2019-04-25 09:45
我方法好像和你一样,但是好像只能通过67
点赞 回复
分享
发布于 2019-04-25 09:35
联易融
校招火热招聘中
官网直投
def  main():     line = raw_input()     N = int(line.split()[0])          line = raw_input()     lines = line.split()          label = []     for item in lines:         label.append(int(item))          kinds = set(label)     times = {}          for kind in kinds:         times[kind] = label.count(kind)               num = 1     flag = 0     while True:         if N%num == 0:             avg = N/num             good = 1             for kind in kinds:                 if times[kind]%avg != 0:                     good = 0                     break                          if good == 1:                 flag = 1                      if flag == 1 or num>N/2+1:             break                      num += 1          res = 0     if flag == 1:         res = num     return res
点赞 回复
分享
发布于 2019-04-25 09:40

相关推荐

点赞 3 评论
分享
牛客网
牛客企业服务