1. 程式人生 > >【PAT】1054. The Dominant Color (20)

【PAT】1054. The Dominant Color (20)

題目描述

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解析度的圖片(例如800*600),你需要輸出其嚴格的主色。

INPUT FORMAT

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, 2^24)範圍內的數字顏色。資料保證每張輸入的圖片都存在嚴格的主色。一行內所有數字之間用空格隔開。

OUTPUT FORMAT

For each test case, simply print the dominant color in a line.

翻譯:對於每組輸入資料,一行內輸出主色。

Sample Input:

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

Sample Output:

24

解題思路

將資料進行排序,然後計數,如果過半就輸出。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#define INF 99999999
using namespace std;
int temp,M,N;
int d[500000],ccount=0;
int main(){
    scanf("%d%d",&M,&N);
    int a;
    int Max=M*N;
    for(int i=0;i<Max;i++)scanf("%d",&d[i]);    
    sort(d,d+Max);
    int ccount=0;
    for(int i=0;i<Max;i++){
        if(i==0||d[i-1]!=d[i])ccount=1;
        else ccount++;
        if(ccount>Max/2){
            printf("%d\n",d[i]);
            break;
        }
    }
    return 0;
}