1054.The Dominant Color (20)

1054.The Dominant Color (20)

pat-al-1054

2017-01-26

  • 写了好久的一题,一开始用c写的,全部用数组手动查找,一个测试点超时,一个测试点出错
  • 后来看到一个不错的思路:PAT 解题报告 1054. The Dominant Color (20)自己照着写了一下发现还是有一个测试点超时
  • 于是又找了一个新的方法:1054. The Dominant Color (20)-PAT甲级真题写了发现那个测试点依旧超时,突然想起来cin比scanf耗时大,果然替换之后就不超时了
  • 坑:如果使用了cin、cout,有测试点超时且感觉不是程序有问题,那么换成scanf和printf试试
  • 强迫症的难过:一开始写的那个方法,那个出错的测试点到底是为什么?……可能我永远都不会知道了……
  • map的使用
/**
 * pat-al-1054
 * 2017-01-26
 * Cpp version
 * Author: fengLian_s
 */
#include<map>
using namespace std;
int main()
{
  int m, n;//m是列数,n是行数
  scanf("%d %d", &m, &n);
  int half = m*n/2;
  //cout << "half = " << half << endl;
  map<int, int> color;
  for(int i = 0;i < m*n;i++)
  {
    int tmp;
    scanf("%d", &tmp);//大坑:有一个测试点运行超时是因为用cin输入耗时
    color[tmp]++;
    //cout << tmp << ": " << color[tmp] << endl;
    if(color[tmp] > half)
    {
      printf("%d\n", tmp);
      break;
    }
  }
  return 0;
}

-FIN-

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值