1005 继续(3n+1)猜想 (25 分)
题目入口:https://pintia.cn/problem-sets/994805260223102976/problems/994805320306507776
AC代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
typedef long long LL;
#define inf 0x3f3f3f3f
#define mm(a) memset(a, 0, sizeof(a))
int ret[100];
int pro[5000];
int npos = 1, pos = 0;
void C(int num){
while (num > 1){
if (num%2 == 0){
num /= 2;
pro[pos++] = num;
}
else {
num = (num*3+1)/2;
pro[pos++] = num;
}
}
}
bool cmp(int a, int b){
return a > b;
}
int main() {
int n;
scanf("%d", &n);
int num;
scanf("%d", &num);
ret[0] = num;
C(num);
for (int i = 2; i <= n; i++){
scanf("%d", &num);
int flag = 0;
for (int j = 0; j < pos; j++){
if (num == pro[j]){
flag = 1;
break;
}
}
if (!flag){
ret[npos++] = num;
C(num);
}
}
for (int i = 0; i < npos; i++){
for (int j = 0; j < pos; j++){
if (ret[i] == pro[j]){
ret[i] = 0;
break;
}
}
}
sort(ret, ret + npos, cmp);
int flag = 0;
for (int i = 0; i < npos && ret[i] != 0; i++){
if (flag) printf(" ");
else flag = 1;
printf("%d", ret[i]);
}
return 0;
}