PAT 1005 继续(3n+1)猜想 (25 分)

#include 
#include 
#include 
using namespace std;
bool HashTable[10000];
bool cmp(int a, int b) {return a > b;}
int main() {
    int k, n, flag = 0;
    cin >> k;
    vector v(k);
    for (int i = 0; i < k; i++) {
        cin >> n;
        v[i] = n;
        while (n != 1) {
            if (n % 2!=0) n=3 * n + 1;
            n = n / 2;
            if (HashTable[n] == true) break;
            HashTable[n] = 1;
        }
    }
    sort(v.begin(), v.end(), cmp);
    for (int i = 0; i < v.size(); i++) {
        if (HashTable[v[i]] == 0) {
            if (flag == 1) cout << " ";
            cout << v[i];
            flag = 1;
        }
    }
    return 0;
}

GitHub

你可能感兴趣的:(PAT 1005 继续(3n+1)猜想 (25 分))