PAT 甲级 1023 Have Fun with Numbers(20)(思路分析)
1023 Have Fun with Numbers(20 分)
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.
Input Specification:
Each input contains one test case. Each case contains one positive integer with no more than 20 digits.
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.
Sample Input:
1234567899
Sample Output:
Yes
2469135798
思路:这道题比较简单,难点在于不能使用常用的数据类型(超出范围),这里使用字符串模拟加法,另外,需要比较元素组成,可以用数组记录,这里我排序比较字符串
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main() {
string a, b, c;
int up=0;
cin >> a;
for (int i = a.length() - 1; i >= 0; i--) { //字符串模拟加法器
b = to_string((a[i]-'0') * 2 % 10 + up)+b;
up = (a[i]-'0') * 2 / 10;
}
if (up) //补上进位
b = to_string(up) + b;
c = b;
sort(a.begin(), a.end()); //排序,若数字组成相同,排序后字符串也相同
sort(b.begin(), b.end());
if (a == b)
cout << "Yes" << endl;
else
cout << "No" << endl;
cout << c;
return 0;
}
PAT 甲级 1023 Have Fun with Numbers(20)(思路分析)的更多相关文章
- PAT 甲级 1023 Have Fun with Numbers (20 分)(permutation是全排列题目没读懂)
1023 Have Fun with Numbers (20 分) Notice that the number 123456789 is a 9-digit number consisting ...
- PAT 甲级 1023 Have Fun with Numbers
https://pintia.cn/problem-sets/994805342720868352/problems/994805478658260992 Notice that the number ...
- PAT Advanced 1023 Have Fun with Numbers (20) [⼤整数运算]
题目 Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, ...
- 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 ...
- 1023 Have Fun with Numbers (20 分)
1023 Have Fun with Numbers (20 分) Notice that the number 123456789 is a 9-digit number consisting ...
- PAT甲级:1136 A Delayed Palindrome (20分)
PAT甲级:1136 A Delayed Palindrome (20分) 题干 Look-and-say sequence is a sequence of integers as the foll ...
- 【PAT甲级】1023 Have Fun with Numbers (20 分)
题意: 输入一个不超过20位的正整数,问乘2以后是否和之前的数组排列相同(数字种类和出现的个数不变),输出Yes或No,并输出乘2后的数字. AAAAAccepted code: #define HA ...
- PAT甲题题解-1023. Have Fun with Numbers (20)-大数加法
和1024一样都是大数据的题,因为位数最多要20位,long long最多19位给一个num,求sum=num+num问sum包含的数字,是否是num的一个排列,即数字都一样,只是顺序不同罢了. #i ...
- PAT (Advanced Level) 1023. Have Fun with Numbers (20)
手动模拟一下高精度加法. #include<iostream> #include<cstring> #include<cmath> #include<algo ...
随机推荐
- Windows Server 2012开启磁盘性能计数器
Windows Server 2012默认情况下已经禁用了磁盘性能计数器,打开任务管理器后,无法像Win8一样在性能选项卡中看到“磁盘”使用情况,可能是因为微软考虑到安装此服务器系统的硬件都会非常好, ...
- List,Set,Map集合的遍历方法
List的三种实现:ArrayList(数组) LinkedList(链表) Vector(线程安全) List集合遍历方法: List<String> list = new Arra ...
- java-学习8
方法的声明及使用 public class function { public static void main(String[] args) { printInfo();//调用printInfo( ...
- intellij idea 搜索功能快捷键
intellij idea是一款超智能的编译器,因此在信息资源的搜索功能中给我们的用户提供了很大的帮助.同样作为java编译器的eclipse和myeclipse在搜索方面就比intellij ide ...
- 什么是webFlux
什么是webFlux 左侧是传统的基于Servlet的Spring Web MVC框架,右侧是5.0版本新引入的基于Reactive Streams的Spring WebFlux框架,从上到下依次是R ...
- Steering Behaviors
[Steering Behaviors] 1.Seek 下述的算法是一个基本Seek行为,但不带任何Steering输出的力.在该公式作用下,游戏个体的移动方式是直线型的,如果target的位置变了的 ...
- MySql数据类型范围
[MySql数据类型范围] 参考:http://blog.sina.com.cn/s/blog_4f925fc30102edg8.html
- ServiceWork的五种状态
[ServiceWork的五种状态] installing.installed.activating.activated.redundant 参考:https://developer.mozilla. ...
- ChromDevTools
[ChromDevTools] 1.如何打开DevTools. 在Chrome菜单中选择 更多工具 > 开发者工具 在页面元素上右键点击,选择 “检查” 使用 快捷键 F12 2.切换 Devi ...
- 重工单001800020505在IN表IN_SFCHEADER被过滤 TEMP_REMOVED_ID_IN_DATA
select * from SAP_AFKO WHERE AUFNR='001800020505'; ---有数据SELECT * FROM IN_SFCHEADER WHERE MO_ID ='0 ...