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的更多相关文章

  1. 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) ...

  2. 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 ...

  3. PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642

    PAT (Basic Level) Practice (中文)1055 集体照 (25 分) 凌宸1642 题目描述: 拍集体照时队形很重要,这里对给定的 N 个人 K 排的队形设计排队规则如下: 每 ...

  4. PAT (Advanced Level) Practice 1001 A+B Format (20 分)

    题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805528788582400 Calculate a+b and ...

  5. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  6. PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...

  7. 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 ...

  8. PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1035 Password (20 分) 凌宸1642 题目描述: To prepare for PAT, the judge someti ...

  9. 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 ...

随机推荐

  1. 人物传记-BILL RAY:低谷时的信念,决定你能走多远

    自2018年全球经济危机以来,以工作为重的成年人们一直备受打击.尤其是2020年,全球贸易争端使得经济下滑严重,很多公司倒闭破产,有些人甚至从富豪变成了负债者,走向了人生低谷.其实,每个人都会遇到人生 ...

  2. [转]Ubuntu16 压缩解压文件命令

    原文地址:http://blog.csdn.net/feibendexiaoma/article/details/73739279,转载主要方便随时查阅,如有版权要求,请及时联系. ZIP zip是比 ...

  3. Error running 'tomcat': Unknown error

    免费分享95套java实战项目,不仅有源码还有对应的开发视频,关注公众号『勾玉技术』回复"95"即可获取 无意中发现了一位清华大佬的算法笔记,需要的加公众号 勾玉技术 回复 清华算 ...

  4. cobra-强大的CLI应用程序库

    cobra介绍 Cobra是一个用于创建强大的现代CLI应用程序的库,也是一个用于生成应用程序和命令文件的程序. Cobra用于许多Go项目,如Kubernetes.Hugo和Github CLI等. ...

  5. 玩遍博客网站,我整理了 Hugo 及其流行的风格主题

    搭建博客网站是个人进入互联网世界的最常见方式之一.伴随着网站技术的发展,如何搭建博客网站已经变得非常容易了.当然,你可以选择诸如 新浪博客.CSDN.博客园 之类的大型网站,快速创建依赖于大平台的个人 ...

  6. Java Socket编程基础及深入讲解

    原文链接:https://www.cnblogs.com/yiwangzhibujian/p/7107785.html 原文写的特别好,非常详细,但是禁止转载,那我就不再复制粘贴了! socket实现 ...

  7. 微信小程序:如何删除所有的console.log?

    使用vscode正则匹配,手动去除 1.用vscode打开微信小程序项目 2.Edit-----replace in Files 1. console.log()加了分号 console\.log\( ...

  8. Oracle数据库在给表添加字段的sql中用comment报错

    原因:不同于mysql,Oracle数据库在添加表字段时不能直接用comment,而是单独写一个sql语句,如下: alter table SYS_USER add SENDMSG_LASTTIME ...

  9. 基于QT的全自动超声波焊接机上位机追溯系统(已经在设备上应用)

    应用说明: 本上位机程序是我在做锂电池产线项目的时候开发的,用于采集设备数据以及实现设备自动控制,下位机采用基恩士PLC,超声波机采用上海一家的超声波焊接机,实现电芯极耳的自动焊接,上位在设备焊接过程 ...

  10. Lua C++交互 应用实例步骤(UserData使用)

    一.配置Lua C++交互环境 1.下载Lua 包环境 地址: https://www.lua.org/download.html ,我们这里用的是5.4.2版本. 2.新建C++ 控制台应用程序 3 ...