PAT_Basic --- 1005

来源:互联网 发布:仓鼠多少钱一只淘宝上 编辑:程序博客网 时间:2024/05/21 05:38

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个空格隔开,但一行中最后一个数字后没有空格。输入样例:63 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");}
0 0
原创粉丝点击