[Huawei] 2020 session of the campus recruitment software title 2020-2-19

2048 Minimum number of synthesis

Title Description ■
a numbers game, called 2048, the rules of this game is two identical numbers can be added. For example: two 2 may be added after the addition of 4 plus a number, the number 2 will be reduced by two.
There are a series of numbers, currently known digital quantity. Q. adding it needs at least a few times in order to obtain 2048 (guaranteed title is obtained by adding 2048).
Input Description :
The first line number of samples T, working on behalf of the group will follow later in the test data.
Each set of test data input number 10, representing
the number of 2,4,8,16,32,64,128,256,512,1024.
Each digit number does not exceed 1,024.
Description Output:
For each set of data, outputs a number representing the number of times needed to obtain a minimum of 2048 added.
Input
2
. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1
. 1. 1. 1. 1. 1. 1. 1. 1. 1 2

Output
10
1
personal ideas: As a programmer, to 2,4,8,16,32,64,128,256,512,1024 string of numbers really familiar, so naturally think of the need to use bit operation. These numbers are 1,2,3,4,5,6,7,8,9,10 power of 2, 11 and the target 2048 is just a power of two.

That is the first 11 bit is 0, the need for other types of numbers obtained by adding the above-mentioned 2048, so that the result of 11 to 1. We should try to choose a high sum.

Recursive method

void merge(vector<int> &array, int &times, int index) {
	if (array[9] == 2)
		return;

	// 从大向小合并
	if (array[index] > 1) 
	{
		times += 1;
		array[index] -= 2;
		array[index + 1] += 1;
		merge(array, times, index + 1);
		return;
	}

	if (index > 0)
		merge(array, times, index - 1);
}

int main() {
	int n = 0;

	vector<int> array(10);
	cin >> n;

	for (int i = 0; i < n; ++i) {
		// 接收数据
		for (int j = 0; j < 10; ++j) 
			cin >> array[j];
		
		int times = 1;

		merge(array, times, 8);

		cout << times;
		if (i + 1 != n)
			cout << endl;
	}

	return 0;
}

Cycle method

#include<iostream>
#include<vector>
#include<math.h>
using namespace std;

int main()
{
	vector<int> data(10);
	int ret = 0, n = -1, x;//n个数需要n-1次相加
	cin >> x;
	while (x--){
		for (int i = 0; i < 10; i++)
			cin >> data[i];

		for (int i = 9; ret != 2048; i--)
		{
			if (ret == 2048)
				break;
			else if (i == -1)
			{
				ret += 2;
				n++;
			}
			else
			{
				while (ret != 2048 && data[i] != 0){
					ret += pow(2, i + 1);
					n++, data[i]--;
				}
			}
		}
		printf("%d", n);
	}
	system("pause");
}

The maximum right hand weights

■ Title Description

A deck of cards has 54 cards, which spades, hearts, clubs, diamonds have four kinds of color as points
2,3,4,5,6,7,8,9,10, J, Q, K, a brand and a king (J1) small primary (J2). There follows some kind of poker known brand and type of each card type has the right weight

  单牌:任意张单牌,权重0
  对牌:任意两张点数相同的牌,权重2
  单顺:任意五张点数相连的牌(点数顺序为2,3,4,5,6,7,8,9,10,J,Q,K,A),权重3
  三张:任意三张点数相同的牌,权重4
  四张:任意四张点数相同的牌,权重5
  双王:大王+小王,权重5

Assumed that each sub-hand with a n (0 <ns = 18) different cards, now define the right hand of the weight of the hand in may coexist weight of the card type right and, please count on an every half a new maximum weight value.

Input Description:
The first line of input, expressed hand the n (0 <n <-18)
subsequent input row n, n represents the playing cards, each card in addition to King Wang outer points by color and, where S represents black peach, H represents hearts, C represents a plum, D a block,
such as:
SA represents spades A
H10 represents hearts 10
J1 represents the king
J2 represent Wang
output description: The
output value of the maximum weight hand

Input:
. 6
C2
D3
S4
C5
C6
C4
Output: 3
2,3,4,5,6-cis, 4 a single, total weight 3.

Published 139 original articles · won praise 55 · views 60000 +

Guess you like

Origin blog.csdn.net/Vickers_xiaowei/article/details/104399565