PAT B continue examination of 1005 (3n + 1) guess

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/a029019/article/details/100572843

PAT B continue examination of 1005 (3n + 1) guess

topic:

Kharazi (Callatz) conjecture description has been given in 1001. In this topic, the situation is somewhat more complicated.

When we verify Kharazi guess when, in order to avoid double counting, the number can be recorded at each recursive encountered. For example, when n = 3 for verification, we need to calculate 3,5,8,4,2,1, then when we verify the n = 5,8,4,2, can be determined directly Kharazi guess authenticity, without double counting, because it has the number 4 3 when encountered in the validation, we are called 5,8,4,2 3 "coverage" of the number. We call a number in a column n is the number of "key number", if n can not be covered by other figures in the series.

Now given to a series of numbers to be verified, we only need to verify a few key number of them, you do not have to be repeated to verify the remaining numbers. Your task is to find these key figures, according to output them in descending order.

Input format:
Each test comprises a test input, a first row is given a positive integer K (<100), gives the K line 2 to be authenticated mutually different positive integers n (1 <n≤100 ) values between numbers separated by a space.

Output format:
Each test row for output, in descending order output key figures. Separated by a space between the numbers, but after a row last number with no spaces.

Sample input:
. 6
. 3. 5. 6. 7. 8. 11
Output Sample:
76

Code:

#pragma warning(disable:4996)
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#include <memory.h>
int brrby[20000] = { 0 };
void oushu(int a);
void jishu(int a);
int main(void)
{
	int* array,crrcy[101];
	int n = 0, i = 0, j = 0, temp = 0, k = 0;
	scanf("%d", &n);
	array = (int*)malloc(n * (sizeof(int)));
	for (i = 0; i < n; i++)
	{
		scanf("%d", &array[i]);
	}
	for (i = 0; i < n; i++)
	{
		if (array[i] % 2 == 0)
		{
			oushu(array[i]);
		}
		else
		{
			jishu(array[i]);
		}
	}
	for (i = 0; i <n; i++)
	{
		if (brrby[array[i]] == 0)
		{
			crrcy[k] = array[i];
			k++;
		}
	}
	for (i = 0; i < k -1 ; i++)
	{
		for (j = 0; j < k - 1 - i; j++)
		{
			if (crrcy[j] > crrcy[j + 1])
			{
				temp = crrcy[j + 1];
				crrcy[j + 1] = crrcy[j];
				crrcy[j] = temp;

			}
		}
	}
	for (i = k-1; i > 0; i--)
	{
		printf("%d ", crrcy[i]);
	}
	printf("%d", crrcy[0]);
	free(array);
	return 0;
}
void oushu(int a)
{
	while (a % 2 == 0)
	{
		a = a / 2;
		if (a <= 100)
		{
			brrby[a] = 1;
		}
	}
	jishu(a);
}
void jishu(int a)
{
	if (a == 1)
	{
		brrby[1] = 1;
		return;
	}
	a = a * 3 + 1;
	oushu(a);
}

This question is 25 points in the title, there are relatively more difficult than before, but because it is an extension 1001, so there are many places to learn.

This question is actually to find out when there have been no half-cut operation while the original array and some number, so we can brrby labeled 0 and 1 with an array of tags to determine the number has not occurred, large enough only to pay attention to brrby can.

Cut each half regarded brrby [array [i]] labeled as 1, and finally all the array array brrby is a number of 0 crrcy into the array.

Because the title does not say ordered input, all were first crrcy bubble sort descending ,, final output, pay attention to the format.

Guess you like

Origin blog.csdn.net/a029019/article/details/100572843