[PAT] Basic level 1005 继续(3n+1)猜想

1 题目

卡拉兹(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

2 思路

首先,咱们定义输入的数为Input。
遍历Input,对于Input中的每一个数n进行卡拉兹计算,然后把它能覆盖的数都从Input中移除。
这样最终剩下的数就是关键数(不能被其他数覆盖)。

由于输入的数据范围为1~100,同时考虑到最后输出结果的时候要从大到小进行排序。所以可以利用一个bool类型的vector来存放输入数据。

3 代码实现

/******************************************************************************
 * Copyright 2019 gruminions. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *****************************************************************************/

#include <iostream>
#include <vector>

namespace pat {
namespace basic {
namespace level {

class Solution {
 public:
  static void Solve(std::vector<bool>& existed) {
    int len = existed.size();
    RemoveNonKeyNumber(existed, len);
    Print(existed, len);
  }

 private:
  static void RemoveNonKeyNumber(std::vector<bool>& existed, int len) {
    for (int i = len - 1; i >= 2; --i) {
      if (!existed[i]) {
        continue;
      }

      int j = i;
      while (j != 1) {
        int tmp = 0;
        if (j % 2 == 0) {
          tmp = j / 2;
        } else {
          tmp = (3 * j + 1) / 2;
        }

        if (tmp < len && existed[tmp]) {
          existed[tmp] = false;
        }

        j = tmp;
      }
    }
  }

  static void Print(const std::vector<bool>& existed, int len) {
    bool is_first = true;
    for (int i = len - 1; i >= 1; --i) {
      if (existed[i]) {
        if (!is_first) {
          std::cout << " ";
        } else {
          is_first = false;
        }
        std::cout << i;
      }
    }
  }
};
}  // namespace level
}  // namespace basic
}  // namespace pat

using pat::basic::level::Solution;

int main(void) {
  const int kMaxNum = 100;
  int n = 0;
  std::cin >> n;

  std::vector<bool> existed(kMaxNum + 1, false);
  int curr = 0;
  while (n-- > 0) {
    std::cin >> curr;
    existed[curr] = true;
  }

  Solution::Solve(existed);

  return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值