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#参数传递

    前言 菜鸟去重复之Sql的问题还没有得到满意的答案.如果哪位大哥有相关的资料解释,能够分享给我,那就太谢谢了. 接触C#一年了,感觉很多东西还是很模糊,像C#中的委托和事件 有些东西看多了不用也还是不 ...

  2. Postgres-XL9.5r1.6 搭建

    Postgres-XL9.5r1.6 安装部署1,环境准备 关闭防护墙 关闭selinux 下载依赖 yum install -y flex bison readline-devel zlib-dev ...

  3. wp调用百度服务api

    通过百度开放平台申请api成功后,百度会提供一个application key简称ak和一个security key简称sk. 看一下某个服务url的格式 1. url前缀 2. 服务类型 3. 参数 ...

  4. TSQL--逻辑查询处理

    1. 查询处理可分成逻辑处理和物理处理,逻辑处理上各阶段有特定的顺序,但为优化查询,在保证结果集正确的条件下,物理处理顺序并不按照逻辑处理顺序执行,如果在INNER JOIN时,WHERE语句中的过滤 ...

  5. Hadoopd 单元测试-MPUnit

    Apache 版: 官网:http://mrunit.apache.org 教程:https://cwiki.apache.org/confluence/display/MRUNIT/MRUnit+T ...

  6. CSS content应用

    一.简介 content属性早在 CSS2.1的时候就被引入了,可以使用:before以及:after伪元素生成内容.此特性目前已被大部分的浏览器支持:(Firefox 1.5+, Safari 3. ...

  7. JavaScript多个h5播放器video,点击一个播放其他暂停

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

  8. yield 学习

    什么是生成器 生成器是可以迭代的,但是你只可以读取它一次 ,因为它并不把所有的值放在内存中,它是实时地生成数据. yield 理解 通常的for...in...循环中,in后面是一个数组,这个数组就是 ...

  9. MySQL 跟中文相关

    convert ()

  10. Apache环境修改.htaccess文件实现子目录强制HTTPS访问

    如果要在Apache环境下实现子目录强制HTTPS地址访问,该怎么实现呢?在此文章中将与大家一起分享如何在Apache环境下修改.htaccess文件来实现子目录强制HTTPS地址访问. 1.根目录域 ...