重新用大佬的方法,写了一遍,在visual2019编译无问题,但在oj系统内发现出现段错误,尚不清楚原因......
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<functional>
using namespace std;
int cov[100];
bool cmp(int a,int b)
{
return a > b;
}
void fun(int m)
{
cov[m] = 1;//存入覆盖数,令为1
if (m == 1)
return ;
if (m % 2 == 0)
fun(m / 2);
else
fun((3 * m + 1) / 2);
}
int main()
{
int n;
cin >> n;
int test[100];
for (int i = 0; i < n; i++)
{
cin >> test[i];
if (test[i] % 2 == 0)
fun(test[i] / 2);
else
fun((3 * test[i] + 1) / 2);//多判断一次,目的是防止初始测试数存入覆盖数内
}
int res[10],k=0;
for (int i = 0; i < n; i++)
{
if (cov[test[i]] != 1)
{
res[k] = test[i];
k++;
}
}
sort(res, res + k ,cmp);//std::greater<int>()降序排列
for (int i = 0; i < k; i++)
{
cout << res[i] ;
if (i != k - 1)
cout << " ";
}
}