当前位置:网站首页>PAT乙级-B1005 继续(3n+1)猜想分数(25)【map解决】
PAT乙级-B1005 继续(3n+1)猜想分数(25)【map解决】
2022-07-17 23:50:00 【nekoha_dexter】
卡拉兹(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<map>
using namespace std;
//利用map自动排序和find()的特点,完整剔除元素和从大到小输出
//1、用map存放待验证的正整数(1<n<=100),map可以从大到小排序
//2、遍历map取出一个数后,开始进行验证,用iscal[]记录是否已验证了。
//2.1、验证时有可能大于100,例如(99*3+1)/2 >100,此时必不可能在map中
//2.2、利用iscal[],如果验证过的不必再进行剔除,如验证3时,
//5、8、4、2、1再iscal[]中置为true,不必进行剔除操作
//2.3、剔除时可以用map.find()和map.erase()进行操作
//3、由于map从大到小排序,遍历验证完后直接输出
bool iscal[101];//记录是否已进行计算
//map从大到小排序,存入待验证的正整数
map<int, bool, greater<int>> table;
int main(){
int k,n;
cin >> k;
for(int i = 0; i < k;++i){
cin >> n;
table[n] = true;//放入map
}
for(auto it = table.begin(); it != table.end(); ++it){
int t = it->first;
while(t /2 != 1){
if(t & 1) t = (3 * t + 1)/2; //若为奇数
else t /=2;
if(t > 100) continue; //大于100的均不用剔除和记录
if(iscal[t]) break; //false时,表示未验证过,可以查找并剔除
iscal[t] = true;
map<int,bool> :: iterator pt = table.find(t); //查找并剔除操作
if(pt != table.end()) table.erase(pt);
}
}
auto it = table.begin();
cout << it->first;//输出第一个元素,且至少有一个输出结果
it++;
while(it != table.end()){//随后的元素如有,则先输出空格再输出元素
cout << " " << it->first;
it++;
}
return 0;
}边栏推荐
- OSPF comprehensive experiment
- Codeforces round 807 (Div. 2) e. mark and Professor Koro binary / segment tree
- CarMaker快速入门第四课开发48V P1混动系统
- [gym103660] the 19th Zhejiang University City College Programming Contest vp/s
- Wechat applet 6 cloud development cloud database
- 02-线性结构3 Reversing Linked List
- 浅谈ISP-CCM
- 马走斜日(回溯法)
- DevOps工具链:开放、自由地选择最适合团队和业务需要的工具
- 队列的实现(循序存储,链式存储)
猜你喜欢

emoji 为什么叫 emoji

Notepad++ practical function sharing (regular line end line beginning replacement common methods, text comparison functions, etc.)

代码合规性:开发人员使用Helix QAC的5大原因

工作笔记|聊聊数据质量稽核

Devops tool chain: open and free to choose the tools most suitable for the needs of the team and business

PostgreSQL in Linux and windows installation and introductory basic tutorial

Wechat applet 8 cloud function

C# wpf 使用ListBox实现尺子控件

Notepad++实用功能分享(正则行尾行首替换常用方法、文本比对功能等)

Upload files to remote devices through pyro4 command parameters
随机推荐
【codeforces round#800 D题 Fake Plastic Trees】树上贪心
Arkui FAQ summary [Series 2]
lambda函数以及对 items.sort(key = lambda y:y[1], reverse = True) 的理解。
2021牛客多校训练营5(B题)
Redis high frequency interview questions full version
【用户文章】P4合并实践指南之实例拆解Resolve
2036: [蓝桥杯2022初赛] 统计子矩阵(二维前缀和,一维前缀和)
手机买股票开户哪家券商公司好?哪个更安全
postman调用接口返回404的几种原因
Wechat applet 6 cloud development cloud database
網絡上開戶買基金是否安全呢?剛接觸基金,不懂求指導
test
Is it safe to buy funds in a securities account. Can you make a short line
Wechat applet 8 cloud function
浅谈ISP-噪声模型1
Is it safe for xinjimin to buy funds on Alipay
GYM103660E. Disjoint path on tree count
1.3.1 全排列问题
GYM103660H. Distance
Notepad++ practical function sharing (regular line end line beginning replacement common methods, text comparison functions, etc.)