PAT甲級練習題(2)The Dominant Color Colors in Mars (20)

原題:The Dominant Color (20)

鏈接:https://www.nowcoder.com/questionTerminal/0495013675774f008541ea371eb5af17
來源:牛客網

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.

輸入描述:

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.

輸出描述:

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

示例1
輸入

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

輸出

24

翻譯:

鏈接:https://www.nowcoder.com/questionTerminal/0495013675774f008541ea371eb5af17
來源:牛客網

在計算機內存的幕後,對於每個像素,色彩總是被稱爲一系列24位信息。在圖像中,具有最大比例區域的顏色稱爲主色。嚴格占主導地位的顏色佔據了總面積的一半以上。現在給定分辨率爲M乘N的圖像(例如800x600),您應該指出嚴格的主色。

輸入描述:

每個輸入文件包含一個測試用例。對於每種情況,第一行都包含2個正數:M(<= 800)和N(<= 600),它們是圖像的分辨率。然後,接着N行,每行包含[0,224)範圍內的M個數字顏色。確保每個輸入圖像都存在嚴格的主色。一行中的所有數字都用空格分隔。

輸出描述:

對於每個測試用例,只需在一行中打印主要顏色即可。

示例1
輸入

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

輸出

24

思路:

一句話,找出給定數據流的衆數。

麻煩做法:

創建一個很大的二維數組,遍歷,存儲每次出現的數字以及他的次數,最後比較次數,次數最多的就是要找的數。

代碼:


#include <iostream>
#include <string>
#include <cmath>
#include <stack>
using namespace std;
void init() {}
int main()
{
	int n = 0, m = 0;
	int res = 0, num = 0,number=0;
	cin >> n >> m;
	for (int i = 0; i < n*m; i++)
	{
		cin >> num;
		if (number==0)
		{
			res = num;
			number++;
		}
		else if (res!=num)
			number--;
		else
			number++;
	}
	cout<<res<<endl;

	return 0;
}

學習大佬寫的代碼,原理未知,不過這樣確實減少了空間複雜度,就是O(1).

原題:Colors in Mars (20)

題目描述

People in Mars represent the colors in their computers in a similar way as the Earth people. That is, a color is represented by a 6-digit number, where the first 2 digits are for Red, the middle 2 digits for Green, and the last 2 digits for Blue. The only difference is that they use radix 13 (0-9 and A-C) instead of 16. Now given a color in three decimal numbers (each between 0 and 168), you are supposed to output their Mars RGB values.

輸入描述:

Each input file contains one test case which occupies a line containing the three decimal color values.

輸出描述:

For each test case you should output the Mars RGB value in the following format: first output “#”, then followed by a 6-digit number where all the English characters must be upper-cased. If a single color is only 1-digit long, you must print a “0” to the left.

輸入例子:

15 43 71

輸出例子:

#123456

翻譯:

譯文描述

火星人以與地球人相似的方式在計算機中表示顏色。 也就是說,顏色由6位數字表示,其中前2位是紅色,中間2位是綠色,後2位是藍色。 唯一的區別是它們使用的是基數13(0-9和A-C)而不是16。現在給定顏色爲三個十進制數字(每個數字介於0和168之間),您應該輸出其Mars RGB值。

輸入描述:

每個輸入文件包含一個測試用例,該用例佔一行包含三個十進制顏色值。

輸出描述:

對於每個測試用例,您應該以以下格式輸出Mars RGB值:首先輸出“#”,然後輸出6位數字,其中所有英文字符都必須大寫。 如果單色只有1位數字長,則必須在左側打印“ 0”。

輸入例子:

15 43 71

輸出示例:

#123456

思路:

輸入數字,處理,輸出。注意是13進制。

代碼:

#include <iostream>
#include <string>
#include <cmath>
#include <stack>
using namespace std;
int num[20] = { 0 };
int number = 0;
stack<int> s; 
void init(int a) {
	int t = a;
	int temp = 0;
	while (a!=0)
	{
		temp = a % 13;
		s.push(temp);
		a = a / 13;
	}
	int res = 0;
	while (!s.empty())
	{
		temp = s.top();
		s.pop();
		/*res = res + pow(10, s.size()-1);*/
		//cout << temp ;
		if (temp<10&&t<9)
            //輸出兩位  所以判斷是否需要輸出0
		{
			num[number++] = 0;
		}
		num[number++]=temp;
	}
	//cout <<"  ";
}
char a[13] = { '0','1','2','3','4','5','6','7','8','9','A','B','C' };
int main()
{
	int n1, n2, n3;
	cin >> n1 >> n2 >> n3;
	init(n1);
	init(n2);
	init(n3);
	cout << "#";
	for (int i = 0; i <number; i++)
	{
		cout << a[num[i]];
	}
	cout <<endl;


	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章