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;
}
发布了96 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41852212/article/details/102714792
今日推荐