PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642
PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642
题目描述:
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, with no duplication. Double it we will obtain 246913578, which happens to be another 9-digit number consisting exactly the numbers from 1 to 9, only in a different permutation. Check to see the result if we double it again!
Now you are suppose to check if there are more numbers with this property. That is, double a given number with k digits, you are to tell if the resulting number consists of only a permutation of the digits in the original number.
译: 注意到数字 123456789 是一个恰好由数字 1 到 9 组成的没有重复数字的 9 位数。将它翻倍我们将会得到 246913578 , 刚好也是一个由数字 1 到 9 组成的没有重复数字的 9 位数的一个不同排列。如果我们再翻一倍看看结果如何!
现在你要检查是否更多的数字具有这个性质。给定一个 k 位数,将其翻倍,你应该告诉这个结果的数字是否是由原先数字的不同的排列组成。
Input Specification (输入说明):
Each input contains one test case. Each case contains one positive integer with no more than 20 digits.
译:每个输入文件包含一个测试用例,每个用例包含一个不超过 20 位的正整数。
Output Specification (输出说明):
For each test case, first print in a line "Yes" if doubling the input number gives a number that consists of only a permutation of the digits in the original number, or "No" if not. Then in the next line, print the doubled number.
译:对于每个测试用例,如果将输入的数字翻倍后的结果是由原先数字的一个不同排列,在第一行中输出 Yes
,否则输出 No
。在接下来的一行中,输出翻倍后的数字。
Sample Input (样例输入):
1234567899
Sample Output (样例输出):
Yes
2469135798
The Idea:
设计到大整数(超过 long long
范围)的运算,虽然只是一个简单的翻倍,所以需要用数组来存储输入数据,为了在输出时更方便,我选择了直接使用 string 类型存储输入数据和输出结果。
对于大整数的加倍,从最后一位开始,将每一位数翻倍,对应位置上的结果如果大于等于 10
,则进位。
对于比较两者是否是一个同一群数字的不同排列,我又偷懒的选择了利用 map
,直接统计每个数字出现的次数,如果每个数字出现的次数都相同,那么显然是同一个排列。
The Codes:
#include<bits/stdc++.h>
using namespace std;
map<char , int > mp1 , mp2;
int main(){
string s , ans = "";
cin >> s ;
int temp , flag = 0 ;
for(int i = s.size() - 1 ; i >= 0 ; i --){
mp1[s[i]] ++ ; // 记录输入数字的每个数字出现的次数
temp = (s[i] - '0') * 2 + flag ; // 计算该位置上每位数的翻倍结果,加上低位来的进位
ans += to_string(temp % 10) ; // 拼接到结果中
flag = temp / 10 ; // 重新计算 进位
}
if(flag) ans += to_string(flag) ;
for(int i = ans.size() -1 ; i >= 0 ; i --) mp2[ans[i]] ++ ; // 记录结果的每个数字出现的次数
printf((mp1 == mp2)?"Yes\n":"No\n"); // 判断是否是同样数字的不同排列
reverse(ans.begin() , ans.end()) ; // ans 倒置才是翻倍结果
cout<< ans <<endl;
return 0;
}
PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642的更多相关文章
- PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642
PAT (Advanced Level) Practice 1031 Hello World for U (20 分) 凌宸1642 题目描述: Given any string of N (≥5) ...
- PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642
PAT (Advanced Level) Practice 1002 A+B for Polynomials (25 分) 凌宸1642 题目描述: This time, you are suppos ...
- PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642
PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642 题目描述: 拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下: 每 ...
- PAT (Advanced Level) Practice 1001 A+B Format (20 分)
题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 Calculate a+b and ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...
- PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642
PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...
- PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642
PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...
- PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642
PAT (Advanced Level) Practice 1027 Colors in Mars (20 分) 凌宸1642 题目描述: People in Mars represent the c ...
随机推荐
- GitHub rename the default branch from master to main
GitHub rename the default branch from master to main master => main Repository default branch Cho ...
- ESLint & .eslintignore
ESLint & .eslintignore https://stackoverflow.com/questions/42250257/disable-eslint-rules-for-fol ...
- text to JSON
text to JSON GeoLocaltion API https://www.cnblogs.com/xgqfrms/p/13283680.html https://repl.it/@xgqfr ...
- js web简单的路由管理器
灵感来自此博客和此库 index.html <!DOCTYPE html> <html lang="en"> <head> <meta c ...
- 算法型稳定币USDN有什么魔力引发市场热潮?
最近比特币重新突破了8万大关,区块链行业又再次火爆起来,吸引了圈内圈外人的火热讨论,而这其中市场投资者讨论最频繁的就要属算法型稳定币USDN了. USDN是基于NGK.IO区块链中的稳定币, 1枚US ...
- NGK创造的BGV金融世界观是什么?
目前DeFi已经形成初级的金融体系.笔者将DeFi市场对比传统金融世界观,不过市场结构有显着差异.我们可以这样想:如果在去中心化金融世界中借钱,那么可以把MakerDAO比作为中央银行(+回购). 去 ...
- python 第三方库paramiko
目录 介绍 三种常用方式 使用密码进行登录 使用密钥免密码登录 SFTP 传输文件 terminal demo 介绍 paramiko是什么可以参考其他人的博客或文章,这里不再赘述,直入正题. 本次测 ...
- [Python] Matplotlib 图表的绘制和美化技巧
目录 在一张画布中绘制多个图表 加图表元素 气泡图 组合图 直方图 雷达图 树状图 箱形图 玫瑰图 在一张画布中绘制多个图表 Matplotlib模块在绘制图表时,默认先建立一张画布,然后在画布中显示 ...
- MySQL 修改数据表
修改数据表: 创建数据表 更改表明 更改字段数据类型 更改字段名称 更改字段名称和数据类型 为表添加新字段 将字段顺序改为第一位 将字段顺序改为另一个字段之后 删除字段 1 use test; 2 3 ...
- 能取值亦能赋值的Python切片
切片,就像面包,给几刀,切成一片一片,可以做成吐司,也可以做成三明治,口味更佳: 列表(list).元组(tuple).字符串(str)都能进行切片,得到子片段,实际上切片操作比想象的要强大很多,能取 ...