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. [C#] ??雙問號的意思及用法

    int? x = null; int y = x ?? -1; 上面二行中,第一行是將x變數放入null,為什麼int能放null,可以參考另一篇文章http://charleslin74.pixne ...

  2. WP8.1StoreApp(WP8.1RT)---MessageBox与MessageDialog

    在WP7和WP8中,MessageBox是跟WinForm中一样常用的对话框,但是有一个显著的缺点,就是WP7/8中默认的MessageBox是阻塞线程的.也许是由于这个原因,WP8.1/Win8中采 ...

  3. “全栈2019”Java多线程第三章:创建多线程之实现Runnable接口

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  4. CTF 介绍及杂项

    CTF(Capture The Flag)中文一般译作夺旗赛,在网络安全领域中指的是网络安全技术人员之间进行技术竞技的一种比赛形式.CTF起源于1996年DEFCON全球黑客大会,以代替之前黑客们通过 ...

  5. js的window对象

    js的window对象 1.子窗口方法 function testOpen(){ window.open('son.html','newwindow','height=400, width=600, ...

  6. NSObject 中执行Selector 的相关方法

    1. 对当前Run Loop中Selector Sources的取消 NSObject中的performSelector:withObject:afterDelay:方法将会在当前线程的Run Loo ...

  7. find查找文件命令 - Linux系统中的常用技巧整理

    “find”在Linux系统中是比较常用的文件查找命令,使用方法有很多,可以拥有查找文件.文件目录.文件更新时间.文件大小.文件权限及对比文件时间.下面是整理的“find”常用方法,方便以后需要的时候 ...

  8. ubuntu下安装ffmpeg

    sudo add-apt-repository ppa:kirillshkrogalev/ffmpeg-next sudo apt-get update sudo apt-get install ff ...

  9. TX2 刷机时遇到Parsing board information failed

    因为之前调试I2C时,修改了EEPROM Layout,所以,在刷机时遇到此问题. 解决办法是按照此文档中的介绍来修改布局. 实际操作时,我拿了一块正常的TX2,按照指令: sudo i2cdump ...

  10. 利用Android Studio编写 Android上的c与c++程序

    利用Android Studio编写 Android上的c与c++程序 (2017-05-22 19:01:20) 转载▼ 标签: android 分类: Android开发 原文链接: http:/ ...