1005 继续(3n+1)猜想 (25分)
卡拉兹(Callatz)猜想已经在1001中给出了描述。在这个题目里,情况稍微有些复杂。
当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数。例如对 n=3 进行验证的时候,我们需要计算 3、5、8、4、2、1,则当我们对 n=5、8、4、2 进行验证的时候,就可以直接判定卡拉兹猜想的真伪,而不需要重复计算,因为这 4 个数已经在验证3的时候遇到过了,我们称 5、8、4、2 是被 3“覆盖”的数。我们称一个数列中的某个数 n 为“关键数”,如果 n 不能被数列中的其他数字所覆盖。
现在给定一系列待验证的数字,我们只需要验证其中的几个关键数,就可以不必再重复验证余下的数字。你的任务就是找出这些关键数字,并按从大到小的顺序输出它们。
输入格式:
每个测试输入包含 1 个测试用例,第 1 行给出一个正整数 K (<),第 2 行给出 K 个互不相同的待验证的正整数 n (1)的值,数字间用空格隔开。
输出格式:
每个测试用例的输出占一行,按从大到小的顺序输出关键数字。数字间用 1 个空格隔开,但一行中最后一个数字后没有空格。
输入样例:
6 3 5 6 7 8 11
输出样例:
7 6
法一:vector数组的hash思想
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int n;
while (cin >> n)
{
if(n>=100)break;
//init();
vector<int>a(250,0);
int max = 0;
for (int i = 0; i < n; i++)
{
int x;
cin >> x;
if (x > max)max = x;
a[x] = 1;
}
for (int i = 0; i <= max; i++)
{
int t = i;
if (a[t] == 1)
{
while (t != 1)
{
if (t % 2 == 0)
{
t = t / 2;
}
else
{
t= (3 * t + 1) / 2;
}
if (a[t] == 1)
a[t] = 0;
}
}
}
int count = 0;
for (int i = max; i >= 0; i--)
{
if (a[i] == 1)
{
count++;
if (count == 1)
cout << i;
else
cout << ' ' << i;
}
}
}
return 0;
} 法二:直接map,且利用map的直接按键大小排序的性质,从尾输出
#include<iostream>
#include<map>
using namespace std;
void isx(int& n)
{
if(n%2==0)
n/=2;
else
n=(3*n+1)/2;
}
int main()
{
int n,x;
map<int,int>m;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>x;
m[x]=1;
}
map<int,int>::iterator it=m.begin();
while(it!=m.end())
{
n=it->first;
while(n!=1)
{
isx(n);
if(m.find(n)!=m.end())
m[n]=0;
}
it++;
}
int cnt=0;
for(;it!=m.begin();it--)
{
if(it->second==1)
{
cnt++;
if(cnt!=1)
cout<<" ";
cout<<it->first;
}
}
if(it->second==1)
cout<<" "<<it->first;
return 0;
} 
京公网安备 11010502036488号