pat_1005. Continue (3n+1) conjecture (+) C + + solution __c++

Source: Internet
Author: User
1005. Continue (3n+1) conjecture

Title: https://www.patest.cn/contests/pat-b-practise/1005

Caraz (Callatz) conjecture has been described in 1001. In this topic, the situation is slightly complicated.

When we verify the Caraz conjecture, in order to avoid repeated computations, we can record the number of times encountered in the recursion process. For example, when n=3 is validated, we need to compute 3, 5, 8, 4, 2, 1, when we to n=5, 8, 4, 2 to verify the time, we can directly determine the authenticity of the Caraz conjecture, and do not need to repeat the calculation, because the 4 number has been in the validation of 3 encountered, we call 5, 8, 4, 2 is the number of 3 "overlays". We call a number N in a series as "critical", if n cannot be overwritten by other numbers in the series.

Now given a series of numbers to be validated, we just need to validate a few of the key numbers, so we don't have to repeat the remaining numbers. Your task is to find these key numbers and output them in order from big to small.

input Format: Each test entry contains 1 test cases, line 1th gives a positive integer k (<100), and line 2nd gives the value of K-distinct positive integer n (1<n<=100), separated by a space between the digits.

output format: the output of each test case occupies one row, outputting key numbers in order from large to small. Numbers are separated by 1 spaces, but there are no spaces after the last number in a row.

Input Sample:

6
3 5 6 7 8 11
Output Sample:
7 6


Use C + + list class

#include <iostream>
#include <list>
using namespace std;

int main ()
{
	list<int> currentlist;
	List<int> inputlist;
	int n;
	CIN >> N;
	int x;
	for (int i = 0; i < n; i++)
	{
		cin >> x;
		Inputlist.push_back (x);
	}
	for (List<int>::iterator it = Inputlist.begin (); it!= inputlist.end (); ++it)
	{
		Currentlist.push_back ( *it);
		while (Currentlist.back ()!= 1)
		{
			currentlist.push_back (currentlist.back ()% 2 = 0)? Currentlist.back ()/2: ((Currentlist.back ()) *3+1)/2);
			Inputlist.remove (Currentlist.back ());
		}
		Currentlist.clear ();
	}
	Inputlist.sort ();
	List<int>::iterator ito = Inputlist.end ();
	ito--;
	while (*ito!= Inputlist.front ())
	{
		cout << *ito<< "";
		ito--;
	}
	cout << *ito;
	return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.