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

解题思路:
  给定一个数,乘以2之后,各个数位的出现次数,是否与乘之前是否相同,相同输出Yes,否认输出No,然后下一行打印出乘2之后的各个数位。
  这个题目是属于超大数运算的类型,题目给出的最高数位是20位,那么即便是用最高的unsigned long long 也会面临溢出的情况,所以输入和输出,只能用string,诸位乘2,然后再记录每一位出现的次数,相比较就行。

大数乘法!全排列的意思是:各个数字出现的次数分别相同!

AC代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. char a[];
  4. char b[];
  5. int ori[];
  6. int ne[];
  7. int main(){
  8. memset(ori,,sizeof(ori));
  9. memset(ne,,sizeof(ne));
  10. cin>>a;
  11. //先反着放
  12. int l=strlen(a);
  13. for(int i=l-;i>=;i--){
  14. b[l-i]=a[i];
  15. ori[a[i]-'']++;
  16. }
  17. //大数乘法
  18. int x=;
  19. for(int i=;i<=l;i++){
  20. int y=b[i]-'';
  21. y*=;y+=x;
  22. b[i]=y%+'';
  23. x=y/;
  24. ne[b[i]-'']++;
  25. }
  26. int l2=l;
  27. if(x!=){
  28. l2++;
  29. b[l2]=x+'';
  30. ne[b[l2]-'']++;
  31. }
  32. //判断
  33. int f=;
  34. for(int i=;i<=;i++){
  35. if(ne[i]!=ori[i]){
  36. f=;
  37. break;
  38. }
  39. }
  40. if(f){
  41. cout<<"Yes"<<endl;
  42. }else{
  43. cout<<"No"<<endl;
  44. }
  45. for(int i=l2;i>=;i--){
  46. cout<<b[i];
  47. }
  48. return ;
  49. }
  1.  

PAT 甲级 1023 Have Fun with Numbers (20 分)(permutation是全排列题目没读懂)的更多相关文章

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

  2. 1023 Have Fun with Numbers (20 分)

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

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

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

  4. PAT 甲级 1023 Have Fun with Numbers(20)(思路分析)

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

  5. PAT 甲级 1054 The Dominant Color (20 分)

    1054 The Dominant Color (20 分) Behind the scenes in the computer's memory, color is always talked ab ...

  6. PAT 甲级 1027 Colors in Mars (20 分)

    1027 Colors in Mars (20 分) People in Mars represent the colors in their computers in a similar way a ...

  7. PAT 甲级 1005 Spell It Right (20 分)

    1005 Spell It Right (20 分) Given a non-negative integer N, your task is to compute the sum of all th ...

  8. PAT 甲级 1058 A+B in Hogwarts (20 分) (简单题)

    1058 A+B in Hogwarts (20 分)   If you are a fan of Harry Potter, you would know the world of magic ha ...

  9. PAT 甲级 1031 Hello World for U (20 分)(一开始没看懂题意)

    1031 Hello World for U (20 分)   Given any string of N (≥) characters, you are asked to form the char ...

随机推荐

  1. 深入理解Kubernetes资源限制:内存

    写在前面 当我开始大范围使用Kubernetes的时候,我开始考虑一个我做实验时没有遇到的问题:当集群里的节点没有足够资源的时候,Pod会卡在Pending状态.你是没有办法给节点增加CPU或者内存的 ...

  2. zend studio 13.6.1汉化包安装方法

    1.这里介绍的是离线汉化包的安装,在线汉化安装总是失败,还是离线的方便 2.汉化安装过程中系统可能会弹出报错,忽略就可以(反正我现在就是忽略了在用着) 下面图片中的链接就是在线安装的链接,不管是在线还 ...

  3. [转载]Java 应用性能调优实践

    Java 应用性能调优实践 Java 应用性能优化是一个老生常谈的话题,笔者根据个人经验,将 Java 性能优化分为 4 个层级:应用层.数据库层.框架层.JVM 层.通过介绍 Java 性能诊断工具 ...

  4. TCP的半连接

    T C P提供了连接的一端在结束它的发送后还能接收来自另一端数据的能力.这就是所谓下载的半关闭.正如我们早些时候提到的只有很少的应用程序使用它.为了使用这个特性,编程接口必须为应用程序提供一种方式来说 ...

  5. linux实操_shell读取控制台输入

    基本语法: read [选项] [参数] 选项: -p 指定读取值时的提示符 -t 指定读取值时等待的时间(秒),如果没有在指定的时间内输入,就不再等待了 参数: 变量:指定读取值的变量名 实例1:读 ...

  6. vscode插件-JavaScript(ES6) Code Snippets 缩写代表含义

    Import and export Trigger Content imp→ imports entire module import fs from 'fs'; imn→ imports entir ...

  7. Oracle自动化安装脚本-part01-亲试ok

      #!/bin/bash   node_num=$1 base_config=./network.conf   网络配置文件 software_config=./software.conf  软件包 ...

  8. MySQL数据库有几种索引?分别是什么?

        5种索引     1.主键索引     2.唯一索引     3.普通索引     4.全文索引     5.联合索引

  9. React项目性能优化

    1. 使用生产版本和Fragment 1. 生产版本 确保发布的代码是生产模式下(压缩)打包的代码. 一般运行npm run build命令. 直接从webpack看配置文件,需要设置mode = ' ...

  10. React中生命周期

    1.过时的生命周期(v16.3之前) 1.当前组件初次渲染: 绿色表示执行顺序. constructor(): 如果不需要初始化,可以直接省略,会自动补全该函数. 可以在这个方法中初始化this.st ...