测试点1,3,4有问题
卡拉兹(Callatz)猜想已经在1001中给出了描述。在这个题目里,情况稍微有些复杂。
当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数。例如对 n=3 进行验证的时候,我们需要计算 3、5、8、4、2、1,则当我们对 n=5、8、4、2 进行验证的时候,就可以直接判定卡拉兹猜想的真伪,而不需要重复计算,因为这 4 个数已经在验证3的时候遇到过了,我们称 5、8、4、2 是被 3“覆盖”的数。我们称一个数列中的某个数 n 为“关键数”,如果 n 不能被数列中的其他数字所覆盖。
现在给定一系列待验证的数字,我们只需要验证其中的几个关键数,就可以不必再重复验证余下的数字。你的任务就是找出这些关键数字,并按从大到小的顺序输出它们。
# include<iostream>
# include<algorithm>
# include<vector>
# include<map>
using namespace std;
int cmp(int x, int y){
if(x > y){
return -1;
}
else
{
return 1;
}
}
int main(void){
int sign = 0;
vector<int> V;
int Buf[101];
int count = 0;
map<int, int> M;
int n;
cin >> n;
int x;
for(int i = 0; i < n; i++){
cin >> x;
V.push_back(x);
}
for(vector<int>::iterator i = V.begin(); i != V.end(); i++){
x = *i;
while(x > 1){
if(x % 2 == 0){
M[x] += 1;
x /= 2;
}
else{
M[x] += 1;
x = 3 * x + 1;
M[x] += 1;
x /= 2;
}
}
}
for(vector<int>::iterator i = V.begin(); i != V.end(); i++){
if(M[*i] == 1){
Buf[count++] = *i;
}
}
V.clear();
sort(Buf, Buf+count, cmp);
for(int i = 0; i < count; i++){
if(sign){
cout << " ";
}
else{
sign = 1;
}
cout << Buf[i];
}
return 0;
}
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
bool cmp(int a,int b) {
return a>b;
}
int main() {
vector<int> a(101,0);
int n;
cin>>n;
vector<int> arr(n);
for(int i=0; i<n; i++) {
int p;
cin>>p;
arr[i]=p;
if(!a[p])
while(p!=1) {
int q;
if(p%2)
q=(3*p+1)/2;
else
q=p/2;
if(q<=100){
if(a[q])
break;
a[q]=1;
}
p=q;
}
}
vector<int> A;
for(int i=0; i<n; i++)
if(!a[arr[i]])
A.push_back(arr[i]);
sort(A.begin(),A.end(),cmp);
for(int i=0; i<A.size(); i++)
cout<<A[i]<<(i!=A.size()-1?" ":"");
return 0;
}
// 1005 继续(3n+1)猜想 (25 分).cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
/*
对任何一个正整数 n,如果它是偶数,那么把它砍掉一半;如果它是奇数,
那么把 (3n+1) 砍掉一半。这样一直反复砍下去,最后一定在某一步得到 n=1。
*/
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
//判断 array_value 是否是关键字:如果不是就返回true
bool NotKeyWord(int array_value, int length, int compare_value) {
int temp = array_value;
while (temp != 1) {
if (temp % 2 == 0) {
//偶数
temp = temp / 2;
if (temp == compare_value)
return true;
}
else {
//奇数
temp = (3 * temp + 1) / 2;
if (temp == compare_value)
return true;
}
}
return false;
}
//判断是否是关键字,整个数组中的一个元素进行猜想的遍历,把不是关键字的排除,剩下就是关键字
bool Judge_KeyWord(int* input_array, int index, int length) {
for (int j = 0; j < length; j++) {
if (j != index) {
if (NotKeyWord(input_array[j], length, input_array[index])) {
//说明不是关键字
return false;
}
}
}
return true;
}
int main()
{
//输入数字数量
int number_input, temp = 0;
cin >> number_input;
//数组存放
int input[100];
for (int i = 0; i < number_input; i++) {
cin >> temp;
input[i] = temp;
}
//找出关键字
vector<int> output;
for (int i = 0; i < number_input; i++) {
if (Judge_KeyWord(input, i, number_input)) {
//是关键字
output.push_back(input[i]);
}
}
sort(output.begin(), output.end());
reverse(output.begin(), output.end());
for (int i = 0; i < output.size(); i++) {
cout << output[i];
if (i != output.size()-1) {
cout << " ";
}
}
return 0;
}