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 (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:
1234567899
Sample Output:
Yes
2469135798
题目大意:
就是问给定的一个数字乘以2之后,得到的新数字包括的每一个数字的个数是否与原数字包括的数量同样。题意是比較绕的。说什么乘二又乘二,又什么1-9。事实上。就是仅仅乘了1遍2。数字也能够包括0。数据是比較水的。这也是PAT比不上ACM的地方。网上一组仅仅数1-9的也过了。
解题:
由于数字有20位,long long也不够用。所以就自己手动模拟一下乘2的过程吧。
代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
//计数。存储
int cnt1[10],cnt2[10],num1[25],num2[25],p=0,f=0,x;
string s;
bool flag=true;
memset(cnt1,0,sizeof(cnt1));
memset(cnt2,0,sizeof(cnt2));
memset(num1,0,sizeof(num1));
memset(num2,0,sizeof(num2));
//输入
cin>>s;
for(int i=s.length()-1;i>=0;i--)
{
num1[p++]=s[i]-'0';
}
//模拟乘2
for(int i=0;i<25;i++)
{
x=num1[i]*2+f;
num2[i]=x%10;
f=x/10;
}
//计数
for(int i=0;i<s.length();i++)
{
cnt1[num1[i]]++;
cnt2[num2[i]]++;
}
//特判最高位
if(num2[s.length()])cnt2[s.length()]++;
for(int i=0;i<10;i++)
if(cnt1[i]!=cnt2[i])
{
flag=false;
break;
}
//输出
if(flag)cout<<"Yes\n";
else cout<<"No\n";
flag=false;
//假设是0,那么p没有被赋值,输出0
p=0;
//去前导0
for(int i=24;i>=0;i--)
{
if(num2[i])
{
p=i;
break;
}
}
//输出
for(int i=p;i>=0;i--)
cout<<num2[i];
cout<<endl;
return 0;
}
PAT-中国大学MOOC-陈越、何钦铭-数据结构基础习题集 00-自測4. Have Fun with Numbers (20) 【二星级】的更多相关文章
- 中国大学MOOC-陈越、何钦铭-数据结构-2017春
中国大学MOOC-陈越.何钦铭-数据结构-2017春 学习地址 详细学习内容 Github记录地址 欢迎fork和star,有惊喜值得学习! 参考学习笔记 参考AC代码 数据结构和算法学习笔记 学习内 ...
- 中国大学MOOC-陈越、何钦铭-数据结构-笔记
中国大学MOOC-陈越.何钦铭-数据结构-2017春 跟着<中国大学MOOC-陈越.何钦铭-数据结构-2017春>学习,平时练习一下pat上的作业外:在这里记录一下:平时学习视屏的收获. ...
- 中国大学MOOC中的后台文件传输
早期版本的中国大学MOOC一旦被挂起后,应用在完成当前下载任务后无法继续添加新任务,当然也无法将缓存状态写入数据库.这个问题能否顺利解决直接关系到用户体验. 顺便吐槽下,凡是使用了后台文件传输还提示你 ...
- 中国大学mooc直播回放看这里哦http://www.icourse163.org/forum/1001974001/topic-1003372881.htm?sortType=1&pageIndex=1
中国大学mooc直播回放看这里哦http://www.icourse163.org/forum/1001974001/topic-1003372881.htm?sortType=1&pageI ...
- 中国大学MOOC课程信息之数据分析可视化二
版权声明:本文为博主原创文章,转载 请注明出处:https://blog.csdn.net/sc2079/article/details/82318571 - 写在前面 本篇博客继续对中国大学MOOC ...
- 中国大学MOOC课程信息之数据分析可视化一
版权声明:本文为博主原创文章,转载 请注明出处:https://blog.csdn.net/sc2079/article/details/82263391 9月2日更:中国大学MOOC课程信息之数据分 ...
- 中国大学MOOC课程信息爬取与数据存储
版权声明:本文为博主原创文章,转载 请注明出处: https://blog.csdn.net/sc2079/article/details/82016583 10月18日更:MOOC课程信息D3.js ...
- 中国大学MOOC 邮箱验证的问题
在使用 中国大学 MOOC 过程中,在PC端修改个人资料时,其中有项“常用邮箱”,于是写了QQ邮箱,结果发现一直无法验证,连邮件都无法收到. 经过多番尝试,重新使用邮箱注册的方式注册账号,然后注册成功 ...
- 【python】下载中国大学MOOC的视频
[python]下载中国大学MOOC的视频 脚本目标: 输入课程id和cookie下载整个课程的视频文件,方便复习时候看 网站的反爬机制分析: 分析数据包的目的:找到获取m3u8文件的路径 1. 从第 ...
随机推荐
- Eval函数知识总结
说道Json,我们先来聊聊eval 一.eval是什么?(解析器) eval是一个函数,看本质function eval() { [native code] } 二.怎样使用eval? 语法:str ...
- 安卓开发--ListView
package com.zx.listview01; import java.util.ArrayList; import java.util.HashMap; import java.util.Li ...
- BZOJ 1797 网络流的可行边&必须边
求完网络流以后 tarjan一发 判一判 //By SiriusRen #include <queue> #include <bitset> #include <cstd ...
- Python实现文件阅读功能(Python学习笔记)
#!/usr/bin/python# Filename: filereader.pyimport sys def readfile(filename): '''Print a file to the ...
- 射击的乐趣:WIN32诠释打飞机游戏
一楼留给链接http://blog.csdn.net/crocodile__/article/details/11860129 楼上神贴,膜拜片刻...... 一.游戏玩法和已经实现的功能 1.打开游 ...
- 洛谷P2408 不同子串个数 后缀数组 + Height数组
## 题目描述: 给你一个长为 $N$ $(N<=10^5)$ 的字符串,求不同的子串的个数我们定义两个子串不同,当且仅当有这两个子串长度不一样 或者长度一样且有任意一位不一样.子串的定义:原字 ...
- js数组去重问题
1. 双层循环:外层循环,内层比较值: (1)利用splice直接在原数组进行操作 Array.prototype.delRepeat = function (){ var arr = this; v ...
- vue滚动行为
有人问道如何记录vue页面的滚动条位置,再次载入组件的时候页面滚动到记录的位置? 思路: 记录滚动条位置我们好记 我们要在组件销毁之前也就是页面跳转的时候 需要用到生命周期beforeDistory将 ...
- 洛谷 P2014 选课 && caioj 1108 树形动态规划(TreeDP)3:选课
这里的先后关系可以看成节点和父亲的关系 在树里面,没有父亲肯定就没有节点 所以我们可以先修的看作父亲,后修的看作节点 所以这是一颗树 这题和上一道题比较相似 都是求树上最大点权和问题 但这道题是多叉树 ...
- Draw the RGB data from kinect C++ via opengl
In order to improve my English writing skills,I am going to write the blogs in English form now! -- ...