PAT B 1005 continues (3n + 1) guess (25 points) C language version

(3n + 1) guess (25 points) C language version

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, it 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

Sample output:

7 6

#include <stdio.h>

int cx(int x)
{
    if(x%2==0)
        return x/2;
    else
        return (3*x+1)/2;
}

int sort(int *y,int z)
{
	int temp,b=0;
	for(int j=0;j<z-1;j++)
	{	
		for(int k=0;k<z-1-j;k++)
		{
			if(y[k]<y[k+1])
			{
				temp=y[k];
				y[k]=y[k+1];
				y[k+1]=temp;
				b++;
			}
		}
		if(b==0)
			break;
		else
			b=0;
	}
	return 0;
}

int main()
{
    int n;
    scanf("%d",&n);
    int a[100],b[100]={0};
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(int j=0;j<n;j++)
    {    
        for(int k=0;k<n;k++)
        {
           int t=cx(a[k]);
           while(t!=1)
           {     
               if(a[j]==t)
                {
                    b[j]=1;
                    break;
                }
                else
                    t=cx(t);
           }
            if(b[j]==1)
                break;
        }
    }
    int m=0,c[100];
	for(int l=0;l<n;l++)
    {
		if(b[l]==0)
			c[m++]=a[l];
    }
	sort(c,m);
	for(int d=0;d<m-1;d++)
		printf("%d ",c[d]);
	printf("%d",c[m-1]);
    return 0;
}



After very excited to do it. . .
The code a bit long-winded, can not remember how many use a for loop.
The main idea is: first establish two memory arrays respectively corresponding to the measured elements and marked (labeled 0 is the representative keyword), then all the elements circulating recursion guess (comprising the I code itself) intermediate numbers and a comparison of all the elements of a test, there are "covered" element on the change flag. Finally, mark the elements lined up in descending order, to the output (Note that the last number is not followed by a space).

发布了6 篇原创文章 · 获赞 0 · 访问量 76

Guess you like

Origin blog.csdn.net/weixin_44562957/article/details/104035085