Have Fun with Numbers

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

题目解析
  本题给出一个长度再20以内的数字(由1~9组成),要求判断这个数字加倍后的新数字是不是这个数字的某一种排列,如果是的化输出Yes否则输出No,之后输出加倍后的数字。

  由于数字最大位数为20位,超过了long long int的记录范围我们用数组num记录这个数字,用数组cnt记录num中1~9出现的次数,将num加倍后判断其中1~9出现的次数是否发送改变,若没有发送改变则证明加倍后的数字是原数字的某一种排列,反之则不是。

AC代码

 #include <bits/stdc++.h>
using namespace std;
int num[];
int cnt[];
string str;
void toInt(){
for(int i = ; i < str.size(); i++)
num[i] = str[i] - '';
}
void getCnt(){
for(int i = ; i < str.size(); i++)
cnt[num[i]]++;
}
bool judge(int carry){
if(carry != ) //如果最高位进位不为零,则证明加倍后的数字比原数字多一位,那么其肯定不是原数字的一个排列
return false;
for(int i = ; i < str.size(); i++)
cnt[num[i]]--;
for(int i = ; i <= ; i++){ //判断新的num中1~9的数量是否和加倍前一样
if(cnt[i] != )
return false;
}
return true;
}
int doubleNumber(){ //将数组num加倍并返回最高位进位
int carry = ;
for(int i = str.size() - ; i >= ; i--){
int temp = num[i];
num[i] = ( * temp + carry) % ;
carry = * temp / ;
}
return carry;
}
int main()
{
cin >> str; //输入数字
toInt(); //将输入的数字转化为数组
getCnt(); //获取数组中1~9出现的次数
int carry = doubleNumber(); //将num加倍carry记录最高位的进位
if(judge(carry)){ //判断加倍后的数字是否为原数字的某一个排列
printf("Yes\n"); }else
printf("No\n");
if(carry != ) //判断是否需要输出进位
printf("%d", carry);
for(int i = ; i < str.size(); i++) //输出加倍后的数组num
printf("%d", num[i]);
printf("\n");
return ;
}

PTA (Advanced Level) 1023 Have Fun with Numbers的更多相关文章

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

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

  2. PTA(Advanced Level)1036.Boys vs Girls

    This time you are asked to tell the difference between the lowest grade of all the male students and ...

  3. PTA (Advanced Level) 1004 Counting Leaves

    Counting Leaves A family hierarchy is usually presented by a pedigree tree. Your job is to count tho ...

  4. PTA (Advanced Level) 1020 Tree Traversals

    Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...

  5. PTA(Advanced Level)1025.PAT Ranking

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

  6. PTA (Advanced Level) 1008 Elevator

    Elevator The highest building in our city has only one elevator. A request list is made up with Npos ...

  7. PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous su ...

  8. PTA (Advanced Level) 1006 Sign In and Sign Out

    Sign In and Sign Out At the beginning of every day, the first person who signs in the computer room ...

  9. PTA (Advanced Level) 1003 Emergency

    Emergency As an emergency rescue team leader of a city, you are given a special map of your country. ...

随机推荐

  1. apache mpm的一些问题

    win2003系统下apache环境,mpm_winnt.c模式,优化参数: ThreadsPerChild 说明:每个子进程建立的线程数,默认值:64,最大值:1920.网上查询资料建议设置在100 ...

  2. 执行计划--在存储过程中使用SET对执行计划的影响

    --如果在存储过程中定义变量,并为变量SET赋值,该变量的值无法为执行计划提供参考(即执行计划不考虑该变量),将会出现预估行数和实际行数相差过大导致执行计划不优的情况--如果在存储过程中使用SET为存 ...

  3. Ubuntu 12.04 安装最新版本NodeJS

    昨天搭建了一个Windows NodeJS 运行环境,但Windows 运行NodeJS命令行各种别扭,开源包的编译也是各种问题,折磨了我一天一夜,果断换到Linux 平台.. 我选择了Ubuntu ...

  4. Visual Studio Code 基本操作 - Windows 版

    1.Install the .NET SDK 2.Create app: dotnet new console -o myApp cd myApp 3.Run your app:dotnet run

  5. NG2-我们创建一个可复用的服务来调用英雄的数据

    <英雄指南>继续前行.接下来,我们准备添加更多的组件. 将来会有更多的组件访问英雄数据,我们不想一遍一遍地复制粘贴同样的代码. 解决方案是,创建一个单一的.可复用的数据服务,然后学着把它注 ...

  6. Java 使用json 做配置文件

    概述 经常会用到通过配置文件,去配置一些参数,java里面本来是有配置文件的,但是导入很麻烦的,自从我用了json之后,从此一切配置文件都见鬼去吧. 1.下载gson解析json文件的jar包     ...

  7. Docker 修改镜像源地址

    Docker 官方中国区 https://registry.docker-cn.com 网易 http://hub-mirror.c.163.com ustc https://docker.mirro ...

  8. 如何建立git 远程仓库

    第1步:创建SSH Key.在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步.如果没有,打开Shell ...

  9. AVA + Spectron + JavaScript 对 JS 编写的客户端进行自动化测试

    什么是 AVA (类似于 unittest) AVA 是一种 JavaScript 单元测试框架,是一个简约的测试库.AVA 它的优势是 JavaScript 的异步特性和并发运行测试, 这反过来提高 ...

  10. [CSS3] 动画暗角按钮

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...