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. roadflow asp.net工作流自定义表单

    在roadflow表单设计器不能满足很复杂的业务需求的时候,可以采用自定义表单(即表单页面自己做). 自定义表单就是自己写一个页面,包含控制器视图,然后将这个页面挂到流程上进行审批. 自定义表单分为以 ...

  2. MvvmLight框架使用入门(二)

    上一篇我们简单对MvvmLight做了介绍.罗列了三个DLL中,各个命名空间下主要类的定义及大致作用.因为只是范范的概论,对于从未接触过MvvmLight的萌新来说,根本就是在晃点他们.不过万事开头难 ...

  3. C语言的第零次作业

    C语言--第0次作业 Q1:对于网络专业的了解 一开始我对网络工程这个专业并不是很了解,在报志愿之前,我完全没想过自己会进这个专业,但是经过了一个暑假的时间,我慢慢地开始了解这个学科,并开始对这个专业 ...

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

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

  5. mysql 基础整合大全

    mysql  数据库操作: 创建数据库: create database db_sanguo charset utf8; 切进db_sanguo use db_sanguo 删除数据库: drop d ...

  6. php对ip地址的处理

    public function actions() { $url = "http://ip.taobao.com/service/getIpInfo.php?ip=".self:: ...

  7. [ActionScript 3.0] 记录几个ByteArray 十六进制 String等相互转换的方法

    /** * 通过hax数据返回ByteArray * @param hax 格式 "AA5A000100FF" */ private function getHax(hax:Str ...

  8. python自学之第一章 —— 变量

    1.变量的命名(): (1).可以包含数字.字母.下划线‘_’,但只能以字母和下划线‘_’开头,不能以数字开头! (2).变量的命名不能包含空格. (3).不能将python中的关键字(reserve ...

  9. python基础知识梳理-----1变量,常量,注释

    因为公司用的是delphi ,所以自学了一阵delphi,但是随着语言的发展,delphi的前景堪忧啊.辞职以后打算好好学学python,然后找找数据分析和爬虫方面的工作. 记录此贴,仅作学习使用,所 ...

  10. MySQL之查看数据库编码

    MySQL之查看数据库编码