125

6
3 5 6 7 8 11

7 6

# 代码

## C++解法

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

const int maxn = 105;

bool isKeyNum[maxn];
vector<int> vec;

void Callatz(int n) {
bool isFirst = true;
while (n > 1) {
if (isFirst) {
isFirst = false;
} else {
auto iter = lower_bound(vec.begin(), vec.end(), n);
if (*iter == n) {
int idx = distance(vec.begin(), iter);
isKeyNum[idx] = false;
}
}
if (n % 2) {
n = (n * 3 + 1) / 2;
} else {
n /= 2;
}
}
}

int main() {
// cin.tie(false);
cin.sync_with_stdio(false);

int n;
cin >> n;

vec.clear();
memset(isKeyNum, true, sizeof(isKeyNum));

for (int i = 0; i < n; ++i) {
int a;
cin >> a;
vec.insert(lower_bound(vec.begin(), vec.end(), a), a);
}

for (int i : vec)
Callatz(i);

bool isFirst = true;
for (int i = n - 1; i >= 0; --i)
if (isKeyNum[i]) {
if (isFirst)
isFirst = false;
else
cout << " ";
cout << vec[i];
}
cout << endl;

return 0;
}

## Python解法

def Callatz(n):
global keyNum
isFirst = True
while n > 1:
if isFirst:
isFirst = False
else:
if n in keyNum:
keyNum.remove(n)
if n % 2 == 1:
n = (3 * n + 1) / 2
else:
n /= 2

n = int(input())
lst = [int(i) for i in input().split(' ')]
lst.sort(reverse=True)
keyNum = lst.copy()
for i in range(n):
Callatz(lst[i])
print(" ".join([str(i) for i in keyNum]))

## Java解法

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

class Main {
public static Scanner in;
public static ArrayList<Integer> lst;
public static ArrayList<Boolean> isKeyNum;

public static void Callatz(int n) {
boolean isFirst = true;
while (n > 1) {
if (isFirst) {
isFirst = false;
} else {
int idx = lst.indexOf(n);
if (idx != -1) {
isKeyNum.set(idx, false);
}
}
if (n % 2 == 1)
n = (3 * n + 1) / 2;
else
n /= 2;
}
}

public static void main(String args[]) {
in = new Scanner(System.in);
int n = in.nextInt();
lst = new ArrayList<Integer>();
isKeyNum = new ArrayList<Boolean>();

for (int i = 0; i < n; ++i) {
int a = in.nextInt();
}
Collections.sort(lst);
for (int i : lst) {
Callatz(i);
}
boolean isFirst = true;
for (int i = n - 1; i >= 0; --i) {
if (isKeyNum.get(i)) {
if (isFirst)
isFirst = false;
else
System.out.print(' ');
System.out.print(lst.get(i));
}
}
System.out.print('\n');
in.close();
}
}

• 点击查看/关闭被识别为广告的评论