【PTA-A】1054 The Dominant Color

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805422639136768

题目大意就是每张图像都是由像素构成,像素中包含颜色信息,一张图片中占比最多的颜色就是主导色。

输入:M×N的像素矩阵,然后输入各颜色代表的数字。

输出:找到占比最多的颜色数字。

解题方法:运用map,是一种关键字对应数值的字典。

关于map的用法参考了这个链接https://www.cnblogs.com/panweiwei/p/6657583.html

#include<iostream>
#include<cstdio>
#include<map>
using namespace std;

int main() {
	int m, n, col;
	cin >> m >> n;
	//创建map,这题可以直接使用<int,int>
	map<int, int> M;
	for (int i = 1; i <= n; i++) {
		for (int j = 1; j <= m; j++) {
			cin >> col;
			M[col]++;
		}
	}
	int max = 0, number;
	//创建迭代器
	map<int, int>::iterator iter;
	for (iter = M.begin(); iter != M.end(); iter++) {
		//iter.second代表值,iter.first代表关键字
		if (max < iter->second) {
			max = iter->second;
			number = iter->first;
		}
	}
	cout << number;
	return 0;
}

 

posted @ 2019-09-20 13:11  大帅本帅  阅读(5)  评论(0)    收藏  举报