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. 设置 ssh 使用public key 免密码登录

    第一步,生成自己公钥, 私钥 1: ssh-keygen -t rsa 2:   3: root@yjlml:~# ssh-keygen -t rsa 4: Generating public/pri ...

  2. .NET框架源码解读之启动CLR

    前面提到在SSCLI环境里运行.NET程序的时候,执行的命令类似java程序的执行过程,即通过clix程序解释执行.net程序.这个过程看起来跟在windows环境下执行.net程序表面上看起来不一样 ...

  3. 命令行web客户端与HTTP REST API调试工具

    1.命令行web客户端 curl wget httpie 2.优雅的REST API调试工具 insomnia postman

  4. C#数据结构汇总

    对C#涉及到的数据结构做了一下简单的汇总,若有遗漏,欢迎补充~~ 还是以学习为目的,在此只是简单的介绍一下,希望对大家能有所帮助,能力有限为了不误导大家,不做详细深入的解析,还望见谅,非常欢迎大大们补 ...

  5. leetcode 合并两个有序数组

    给定两个有序整数数组 nums1 和 nums2,将 nums2 合并到 nums1 中,使得 num1 成为一个有序数组. 说明: - 初始化 nums1 和 nums2 的元素数量分别为 m 和 ...

  6. C# AutoMapper的简单扩展

    AutoMapper可以很方便的将一个实体的属性值转化给另一个对象.这个功能在我们日常的编码中经常会遇到.我将AutoMapper的一些基本映射功能做成扩展方法,在编码中更方便使用. using Sy ...

  7. 如何修改Entity Framework Db Frist模式下的Entity继承关系?

    1.准备工作 Db Frist创建实体数据模型(创建edmx并不是重点,各位随意即可),此处取名ZeroCodeDB,所得文件如图所示:其中红框中的文件(ZeroCodeDB.tt)是各实体的生成的关 ...

  8. Mysql 中日期类型bigint和datetime互转

    MySql数据库中字段类型bigint 长度是10位的 mysql> select (from_unixtime(1554047999))as datatime;+--------------- ...

  9. WPF InkCanvas EditingMode为Select时 在其选择时各种事件中撤销Select模式的方法

    InkCanvas有多种输入模式. 通过InkCanvasEditingMode来进行对其调整 分别是 None=0// 忽略鼠标和手写笔输入 Ink = 1// 允许用户绘制批注,默认模式.使用鼠标 ...

  10. fd - 更好的 find 命令

    欢迎关注我的公众号 spider-learn fd(https://github.com/sharkdp/fd) 是 find 命令的一个更现代的替换. 对比一下 查找名字含有某个字符的文件 OLD ...