PAT-中国大学MOOC-陈越、何钦铭-数据结构基础习题集 00-自测4. Have Fun with Numbers (20) 【二星级】
2015-08-19 12:37
791 查看
题目链接:http://www.patest.cn/contests/mooc-ds/00-%E8%87%AA%E6%B5%8B4
题面:
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:
Sample Output:
题目大意:
就是问给定的一个数字乘以2之后,得到的新数字包含的每个数字的个数是否与原数字包含的数量相同。题意是比较绕的,说什么乘二又乘二,又什么1-9。其实,就是只乘了1遍2,数字也可以包含0。数据是比较水的,这也是PAT比不上ACM的地方,网上一组只数1-9的也过了。
解题:
因为数字有20位,long long也不够用,所以就自己手动模拟一下乘2的过程吧。
代码:
题面:
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 differentpermutation. 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; }
相关文章推荐
- 使用C语言构建基本的二叉树数据结构
- 数据结构的各种排序算法稳定性比较
- 二叉树学习笔记-深度和宽度
- 牛人整理分享的面试知识:操作系统、计算机网络、设计模式、Linux编程,数据结构总结
- 散列-分离链接法(数据结构与算法分析-C语言描述)
- typedef关键字与结构体、结构体指针的定义
- HDU 3642 Get The Treasury (线段树扫描线进阶--求长方体重叠3次或以上的体积)
- 数据结构学习笔记5-寻找最小的k个数(选择排序和堆排序)
- HDU 1255 覆盖的面积 (线段树扫描线)
- ACM经典算法之数据结构
- MongoDB学习二--MongoDB 数据结构和查询
- 散列-分离链接法(数据结构与算法分析-C语言描述)
- 数据结构(Java语言)——AVLTree简单实现
- iOS开发——OC常用的数据结构一览。
- 数据结构基本概念
- 数据结构之---C语言实现最短路径之Dijkstra(迪杰斯特拉)算法
- 浅谈数据结构-树和二叉树之间关系
- 浅谈数据结构-二叉树
- POJ 3125 Printer Queue 数据结构 队列
- 2015-8-18数据结构-动态规划-矩阵乘法次数最少