The Dominant Color(PAT)

1.题目描述

Behind the scenes in the computer’s memory, color is always talked about as a series of 24 bits of information for each pixel. In an image, the color with the largest proportional area is called the dominant color. A strictly dominant color takes more than half of the total area. Now given an image of resolution M by N (for example, 800x600), you are supposed to point out the strictly dominant color.
在计算机内存的幕后,颜色总是被认为是一系列24位信息的每一个像素。在图像中,具有最大比例区域的颜色称为主导颜色。绝对占主导地位的颜色占总面积的一半以上。现在给出分辨率为M的N的图像(例如,800x600),您应该指出严格的主导颜色。

2.输入描述:

Each input file contains one test case. For each case, the first line contains 2 positive numbers: M (<=800) and N (<=600) which are the resolutions of the image. Then N lines follow, each contains M digital colors in the range [0, 224). It is guaranteed that the strictly dominant color exists for each input image. All the numbers in a line are separated by a space.
每个输入文件包含一个测试用例。对于每一种情况,第一行包含两个正数:m(<=800)和N(<=600),它们是图像的分辨率。然后N行跟随,每个包含M数字颜色在范围内[0,224)。保证每个输入图像都存在严格的主导颜色。一行中的所有数字都用空格隔开。

3.输出描述:

For each test case, simply print the dominant color in a line.
对于每个测试用例,只需在一行中打印主导颜色。

4.输入例子:

5 3
0 0 255 16777215 24
24 24 0 0 24
24 0 24 24 24

5.输出例子:

24

6.源代码:

#include<stdio.h>
int main()
{
	int i,j,M,N;
	int pixel,dom=0,count=0;
	scanf("%d %d",&M,&N);
	for(i=0;i<M;i++)
		for(j=0;j<N;j++)
		{
			scanf("%d",&pixel);
			if(count==0)//记录主导像素值
			{
				dom=pixel;
				count++;
			}
			else
			{
				if(pixel!=dom)//如果有不相同的像素值
					count--;//比例减少
				else
					count++;//比例增加
			}
		}
	printf("%d",dom);
	return 0;
}
全部评论

相关推荐

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