B1005 Continue (3n + 1) guess (25 points)

1005 Continue (3n + 1) guess (25 points)

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

Output Sample:
76

analysis

An array configuration, recording, how to encounter flag is 1, the output 0 of the sort

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int arr [10000];

bool cmp(int a ,int b){return a>b;
}
int main(){	
	int n,m;
	cin >> n;
	vector<int> v(n);
	for (int i=0;i<n;i++){
		cin >> m;
		v[i] = m;
		while(m!=1){
			if(m%2!=0) m =m*3+1;
			m/=2;
			if(arr[m]==1){
				break;
			}
			arr[m]=1;
		}
	}	
	sort(v.begin(),v.end(),cmp);
	int flag=0;
	for(int i=0;i<v.size();i++){
		if(arr[v[i]]==0)
		{
			if(flag==1) cout <<" ";
			cout << v[i];
			flag = 1;
		}	
	}	
	return 0;
} 

Published 91 original articles · won praise 9 · views 10000 +

Guess you like

Origin blog.csdn.net/WeDon_t/article/details/103791547