PAT_Basic --- 1005

本文介绍了一个基于卡拉兹猜想的编程挑战,重点在于如何通过算法避免重复计算,找到一系列待验证数字中的关键数,并按从大到小的顺序输出。文中提供了一段C++实现代码。

PAT_Basic — 1005

This time, the coding caused me a little disturbing that I thought too much and wasted a lot.

Acctually, its quite simple that if the first number never appearred should be ok and i just checked for the whole list at first.

/*
卡拉兹(Callatz)猜想已经在1001中给出了描述。在这个题目里,情况稍微有些复杂。

当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数。例如对n=3进行验证的时候,我们需要计算3、5、8、4、2、1,则当我们对n=5、8、4、2进行验证的时候,就可以直接判定卡拉兹猜想的真伪,而不需要重复计算,因为这4个数已经在验证3的时候遇到过了,我们称5、8、4、2是被3“覆盖”的数。我们称一个数列中的某个数n为“关键数”,如果n不能被数列中的其他数字所覆盖。

现在给定一系列待验证的数字,我们只需要验证其中的几个关键数,就可以不必再重复验证余下的数字。你的任务就是找出这些关键数字,并按从大到小的顺序输出它们。

输入格式:每个测试输入包含1个测试用例,第1行给出一个正整数K(<100),第2行给出K个互不相同的待验证的正整数n(1<n<=100)的值,数字间用空格隔开。

输出格式:每个测试用例的输出占一行,按从大到小的顺序输出关键数字。数字间用1个空格隔开,但一行中最后一个数字后没有空格。

输入样例:
6
3 5 6 7 8 11
输出样例:
7 6
*/

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct list{
    vector<int> cal_out;
    int num;
};

bool compare(int a, int b)
{
    return a>b;
}

void callatz(int input, int& output){
    double tmp_in = input;
    double tmp = tmp_in / 2;
    if (tmp > (input / 2))
        output = (3 * input + 1) / 2;
    else
        output = input / 2;
}

int check(int n, vector<int> &m){
    for (int i = 0; i < m.size(); i++){
        if (n == m[i])
            return 1;
    }
    return 0;
}

int check2(int n, int m){
    if (n == m)
            return 1;
    else
        return 0;
}

int main(){
    int num_in;
    cin >> num_in;
    int all = num_in;

    vector<int> vec;
    vec.clear();
    list *mul;
    mul = new list[num_in];
    while (num_in--){
        int input, output;
        cin >> input;
        vec.push_back(input);
    }
    num_in = all;
    int i = 0;
    while (num_in--){
        int input, output;

        input = vec[i];
        output = 100;

        while (output != 1){
            callatz(input, output);
            input = output;
            mul[i].cal_out.push_back(output);
            mul[i].num = i;
        }
        i++;

        //sort(mul[all - num_in].cal_out.begin(), mul[all - num_in].cal_out.end(), compare);
    }
    int num2 = all;
    vector<int> vec_label;
    vec_label.clear();
    i = 0;
    while (num2--){

        int tmp_check = 0;
        for (int ii = 0; ii < all; ii++){
            if (i != ii){
                tmp_check = check(vec[i], mul[ii].cal_out);
                if (tmp_check == 1)
                    break;
            }
        }
        i++;
        vec_label.push_back(tmp_check);
    }

    for (int ii = 0; ii < all; ii++){
        if (vec_label[ii] == 0)
            cout << vec[ii];
    }

    system("pause");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值