PTA 1005- continue (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 formats:

Positive integer value of n (1 <n≤100) Each test comprises a test input, a first row is given a positive integer K (<100), the second line gives the K different from each other to be verified , separated by spaces between numbers.

Output formats:

Each test case output per line, in descending order of 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

Sample output:

7 6

C language code

#include <stdio.h>
#include <stdlib.h>
void sort(int *a, int l)//a为数组地址,l为数组长度。
{
    int i, j;
    int v;
    //排序主体
    for(i = 0; i < l ; i ++){
        for(j = i+1; j < l; j ++)
        {
            if(a[i] > a[j])//如前面的比后面的大,则交换。
            {
                v = a[i];
                a[i] = a[j];
                a[j] = v;
            }
        }
    }
}

int main()
{
    int num;
    scanf("%d",&num);
    int a[num]; //存放数字
    int b[10000]={0}; //存放标记
    int n;
    for(int i=0;i<num;i++)
    {
        scanf("%d",&n);
        a[i]=n;
        while(n!=1)
        {
            if(n%2==0)
            {
                n=n/2;
                if(b[n])
                    break;
                else
                    b[n]=1;
            }
            else
            {
                n=(3*n+1)/2;
                if(b[n])
                    break;
                else
                    b[n]=1;
            }
        }
    }
    sort(a,num);

    int k=0;  //标记是否为第一个
    for(int i=num-1;i>=0;i--)
    {
        if(b[a[i]]!=1){
            if(!k)
            {
                k=1;
                printf("%d",a[i]);
            }
            else
                printf(" %d",a[i]);
        }

    }

    return 0;
}

python code

n = input()
i = input()
list1 = list(map(int , i.split()))
list2 = list1.copy()

def text(n):
    if n % 2 == 0:
        n = n/2
    else:
        n = (3 * n + 1) / 2
    return n

for i in list1:
    a = i
    while a > 1:
        a = text(a)
        if a in list2:
            list2.remove(a)
list2.sort(reverse=True)
for i in list2 :
    if i!=list2[len(list2)-1]:
        print(i,end=' ')
    else:
        print(i)

Guess you like

Origin blog.csdn.net/qq_32188669/article/details/93524135