数据结构练习 00-自测4. Have Fun with Numbers (20)
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 file 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:
1234567899Sample Output:
Yes
2469135798
#include<iostream>
#include<string>
#include <sstream>
using namespace std;
int sort(int a[],int n){
int temp;
for(int i=;i<n;i++){
for(int j=i;j<n;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
return ;
}
int main(){
string num;
stringstream ss;
int size,j=;
cin>>num;
size=num.size();
int *a=new int[size];
int *b=new int[size+];
int *doubleNum=new int[size+];
for(int i=;i<size+;i++){
doubleNum[i]=;
b[i]=;
}
for(int i=;i<size;i++){
a[i]=num[i]-;
}
for(int i=size;i>;i--){
if(a[i-]+a[i-]>=){
doubleNum[i]+=(a[i-]+a[i-])%;
doubleNum[i-]+=;
}else{
doubleNum[i]+=a[i-]+a[i-];
}
}
if(doubleNum[]==){
for(int i=;i<size;i++){
b[i+]=doubleNum[i+];
}
sort(doubleNum,size+);
sort(a,size);
for(int i=;i<size;i++){
if(a[i]==doubleNum[i+]){
j++;
}
}
if(j==size){
cout<<"Yes"<<endl;
for(int i=;i<size;i++){
cout<<b[i+];
} }else{
cout<<"No"<<endl;
for(int i=;i<size;i++){
cout<<b[i+];
} }
}else{
cout<<"No"<<endl;
for(int i=;i<size+;i++){
cout<<doubleNum[i];
}
} }
测试
结果
数据结构练习 00-自测4. Have Fun with Numbers (20)的更多相关文章
- pat00-自测4. Have Fun with Numbers (20)
00-自测4. Have Fun with Numbers (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yu ...
- PAT-中国大学MOOC-陈越、何钦铭-数据结构基础习题集 00-自測4. Have Fun with Numbers (20) 【二星级】
题目链接:http://www.patest.cn/contests/mooc-ds/00-%E8%87%AA%E6%B5%8B4 题面: 00-自測4. Have Fun with Numbers ...
- 自测-4 Have Fun with Numbers
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wit ...
- 00-自测4. Have Fun with Numbers
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wit ...
- PTA 自测-4 Have Fun with Numbers
#include<iostream> #include<string> #include<cstring> #include<vector> using ...
- PTA数据结构 习题3.6 一元多项式的乘法与加法运算 (20分)
一元多项式的乘法与加法运算 https://pintia.cn/problem-sets/434/problems/5865 设计函数分别求两个一元多项式的乘积与和. 时间限制:200 ms 内存限制 ...
- PAT自测_打印沙漏、素数对猜想、数组元素循环右移、数字加倍重排、机器洗牌
-自测1. 打印沙漏() 本题要求你写个程序把给定的符号打印成沙漏的形状.例如给定17个“*”,要求按下列格式打印 ***** *** * *** ***** 所谓“沙漏形状”,是指每行输出奇数个符号 ...
- 压测:celey backend为rabbitmq pk redis
使用celery的backend异步获取结果,本文使用rabbitmq 和 redis分别作为backend,代码对比如下 from celery import Celery, platforms i ...
- 华为CloudIDE免费公测,带你出坑带你飞
你的代码仓库上线了吗?是不是有时候遇到这样的问题? 只想浏览一下代码,却发现线上浏览效果不佳,高亮显示什么的都没有.而在桌面端浏览要需要先同步代码,再用桌面端的IDE打开.尤其是使用git的时候,先要 ...
随机推荐
- 更改Sublimetext3的主题文件,改变某些不喜欢的颜色
使用的主题是Monokai(SL),主题很好看,但是注释和内容选中的颜色看起来跟没有一个样,看起来很淡,所以稍微改一下主题文件的颜色.
- Conditionals with Omitted Operands (x ? : y)
Conditionals with Omitted Operands The middle operand in a conditional expression may be omitted. Th ...
- Android API 文档 离线秒开方法
http://blog.csdn.net/haifengzhilian/article/details/39898627 也是最近才看Android开发,但是,它的API文档无论是在线还是离线的,实在 ...
- hdu 4651 Partition (利用五边形定理求解切割数)
下面内容摘自维基百科: 五边形数定理[编辑] 五边形数定理是一个由欧拉发现的数学定理,描写叙述欧拉函数展开式的特性[1] [2].欧拉函数的展开式例如以下: 亦即 欧拉函数展开后,有些次方项被消去,仅 ...
- Mysql参数详解
1.配置参数 MySQL有两种途径途径了解其的配置参数,一个是MySQL交互模式下的命令SHOW VARIABLES,一个使用mysqladmin variables 查询. MySQL的配置参数分 ...
- Chapter 5 - How to Detect the Collisions
Chapter 5 - How to Detect the Collisions Our hero can fire bullets now, but the bullets are only vis ...
- Boost Build
Window XP + Visual Studio 2008 获取Boost库源码 我们可以从http://www.boost.org/ 上获取boost的源代码.当前最新版本为1.45.0. 解压到 ...
- ubuntu中安装eclipse 分类: android ubuntu linux 学习笔记 2015-07-07 10:19 75人阅读 评论(0) 收藏
上一篇说了安装jdk的事,于是趁热打铁,决定把eclipse也安装了. 下载这一系列就不用说了. 下载完成之后: 然后解压,解压之后文件剪切到/usr/software文件夹中,同时重命名为eclip ...
- Linux下搭建Oracle11g RAC(8)----创建ASM磁盘组
以grid用户创建ASM磁盘组,创建的ASM磁盘组为下一步创建数据库提供存储. ① grid用户登录图形界面,执行asmca命令来创建磁盘组: ② 进入ASMCA配置界面后,单击Create,创建新的 ...
- build/envsetup.sh中hmm、get_abs_build_var、get_build_var解析
function hmm() { # 打印帮助信息 cat <<EOF Invoke ". build/envsetup.sh" from your shell to ...
