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:

  1. 1234567899

Sample Output:

  1. Yes
  2. 2469135798

思路:这道题比较简单,难点在于不能使用常用的数据类型(超出范围),这里使用字符串模拟加法,另外,需要比较元素组成,可以用数组记录,这里我排序比较字符串

  1. #include<iostream>
  2. #include<string>
  3. #include<algorithm>
  4. using namespace std;
  5. int main() {
  6. string a, b, c;
  7. int up=0;
  8. cin >> a;
  9. for (int i = a.length() - 1; i >= 0; i--) { //字符串模拟加法器
  10. b = to_string((a[i]-'0') * 2 % 10 + up)+b;
  11. up = (a[i]-'0') * 2 / 10;
  12. }
  13. if (up) //补上进位
  14. b = to_string(up) + b;
  15. c = b;
  16. sort(a.begin(), a.end()); //排序,若数字组成相同,排序后字符串也相同
  17. sort(b.begin(), b.end());
  18. if (a == b)
  19. cout << "Yes" << endl;
  20. else
  21. cout << "No" << endl;
  22. cout << c;
  23. return 0;
  24. }

PAT 甲级 1023 Have Fun with Numbers(20)(思路分析)的更多相关文章

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

  2. PAT 甲级 1023 Have Fun with Numbers

    https://pintia.cn/problem-sets/994805342720868352/problems/994805478658260992 Notice that the number ...

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

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

  5. 1023 Have Fun with Numbers (20 分)

    1023 Have Fun with Numbers (20 分)   Notice that the number 123456789 is a 9-digit number consisting ...

  6. PAT甲级:1136 A Delayed Palindrome (20分)

    PAT甲级:1136 A Delayed Palindrome (20分) 题干 Look-and-say sequence is a sequence of integers as the foll ...

  7. 【PAT甲级】1023 Have Fun with Numbers (20 分)

    题意: 输入一个不超过20位的正整数,问乘2以后是否和之前的数组排列相同(数字种类和出现的个数不变),输出Yes或No,并输出乘2后的数字. AAAAAccepted code: #define HA ...

  8. PAT甲题题解-1023. Have Fun with Numbers (20)-大数加法

    和1024一样都是大数据的题,因为位数最多要20位,long long最多19位给一个num,求sum=num+num问sum包含的数字,是否是num的一个排列,即数字都一样,只是顺序不同罢了. #i ...

  9. PAT (Advanced Level) 1023. Have Fun with Numbers (20)

    手动模拟一下高精度加法. #include<iostream> #include<cstring> #include<cmath> #include<algo ...

随机推荐

  1. python类和对象的底层实现

    按照python中"一切皆对象的原理",所有创建的对象,都是一个已知存在的class实例化的结果;那么class又是被哪个"类"实例化的呢?先看下面的一段代码 ...

  2. js对象和jquery对象互相转换

    javascript对象转成jquery对象 jquery对象转成javascript对象

  3. ubuntu下搭建node server的几个坑

    [ubuntu下搭建node server的几个坑] 1.环境变量 process.env.PORT需要使用 export PORT=80设置 windows下是set PORT=80 2.命令连结 ...

  4. pandas的日常笔记--查询

  5. MyEclipse2014安装aptana插件

    1. 2. aptana插件下载地址 链接: https://pan.baidu.com/s/1sloiAK1 密码: a1nh 3. 4. 确认是否安装成功

  6. defer和async的详细区别

    看过javascript高级程序设计的人,在javascript高级程序设计里,应该看到了介绍了有关defer和async的区别,可是比较浅显,而且也说得不是很清楚.下面我们来通过图片来详细了解下df ...

  7. 七:python 对象类型详解三:列表

    一:列表简介: 1,列表可以包含任何种类的对象:数字.字符串甚至集合对象类型.列表都是可变对象,它们都支持在原处修改的操作,可以通过指定的偏移量和分片.列表方法调用.删除语句等方法来实现.关键的作用有 ...

  8. SoundChannel和soundTransform的关系

    SoundChannel 音乐的  播放  暂停  当前获取当前位置  长度 leftPeak : Number[只读] 左声道的当前幅度(音量),范围从 0(静音)至 1(最大幅度) rightPe ...

  9. 江西财经大学第一届程序设计竞赛 G题 小Q的口袋校园

    链接:https://www.nowcoder.com/acm/contest/115/G来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  10. HDU-1176.免费馅饼(数字三角形变形)

    看到网上大多都是逆向的总结,我来搞个正向的吧... 这道题想着是和数字三角形差不多的,但是最后愣是没有写出来,感受到一股菜意......哭唧唧.jpg 本题大意: 给定n个序列,每个序列包含两个数表示 ...