Blue Bridge Cup: Reign string can not use hexadecimal conversion

Blue Bridge Cup: Reign string can not use hexadecimal conversion

【Problem Description】

Xiaoming corresponding figure 1 with the letters A, B corresponding to 2, and so on, with the corresponding Z 26. For more than 27 digits, Xiaoming to correspond with two or more strings of bits, for example, corresponding to 27 AA, AB corresponding to 28, AZ corresponding to 52, LQ 329 corresponds.

What Will 2019 corresponding string?

At first glance it seems is to investigate the binary conversion, but later wrote about the program will find:
A = 1, B = 2, ... the Z-26 = yes, 0 no, it is not enough ideas to ordinary base for the conversion of the solution

Thinking

By simple geometry indicated by the letter mathematical knowledge is determined not more than three letters, because 26 is 2 = 676,676 * 3> 2019

So the solution like direct violence, such as the current is equal to the highest level to determine how much it is to find one of the largest digital x makes 26 2 * x is less than equal to 2019
and then the digital transformation: 2019 - 26 = x * 2 to

#include <iostream>

using namespace std;

int a[4];
int len = 0;

int main()
{
	int x = 2019;
	
	for(int i=3; i>=1; i--)
	{
		int radix = 1;
		for(int j=0; j<i-1; j++)
		{
			radix *= 26;
		}
		
		for(int j=26; j>=1; j--)
		{
			if(radix*j <= x)
			{
				a[len++] = j;
				x -= (radix * j);
				break;
			}
		}
	}
	
	for(int i=0; i<len; i++)
	{
		cout<<(char)('A'-1+a[i])<<" ";
	}

	return 0;
}

Here Insert Picture Description

Annex: Error codes using the hexadecimal conversion

Here Insert Picture Description

Published 44 original articles · won praise 1 · views 566

Guess you like

Origin blog.csdn.net/weixin_44176696/article/details/104080440