PAT Advanced 1023 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 diferent 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
题目分析
已知一个最多20位的数字N,判断N乘2后的数字M是否只是组成N的各位数字的重新排列
解题思路
- 用一个数组记录N中各位数字出现的次数,乘2后的数字M中出现相同数字则出现次数减一(该判断隐含M和N的长度不相等的情况,长度不相等则出现次数一定不相等,数组中出现次数不相等时元素值不为0)
- 大整数相乘算法
易错点
- 字符和数字的转换(字符-'0'=数字)
知识点
- 字符串转换
#include <algorithm>
reverse(ds.begin(),ds.end());
Code
Code 01
#include <iostream>
#include <algorithm>
using namespace std;
int main(int argc,char * argv[]) {
string s,ds;
cin>>s;
int sf[10]= {0},carry=0;
for(int i=s.length()-1; i>=0; i--) {
sf[s[i]-'0']++;
int temp=(s[i]-'0')*2+carry;
ds.push_back(temp%10+'0');
carry=temp/10;
sf[temp%10]--;
}
while(carry!=0) {
ds.push_back(carry%10+'0');
sf[carry%10]--;
carry/=10;
}
bool flag = true;
// strrev(s.c_str());
// if(ds.length()!=s.length())flag=false;
// else {}
for(int i=0; i<10; i++) {
if(sf[i]!=0) {
flag = false;
break;
}
}
printf("%s", flag? "Yes\n" : "No\n");
reverse(ds.begin(),ds.end());
printf("%s",ds.c_str());
return 0;
}
Code 02
#include <cstdio>
#include <string.h>
using namespace std;
int book[10];
int main() {
char num[22];
scanf("%s", num);
int flag = 0, len = strlen(num);
for(int i = len - 1; i >= 0; i--) {
int temp = num[i] - '0';
book[temp]++;
temp = temp * 2 + flag;
flag = 0;
if(temp >= 10) {
temp = temp - 10;
flag = 1;
}
num[i] = (temp + '0');
book[temp]--;
}
int flag1 = 0;
for(int i = 0; i < 10; i++) {
if(book[i] != 0)
flag1 = 1;
}
printf("%s", (flag == 1 || flag1 == 1) ? "No\n" : "Yes\n");
if(flag == 1) printf("1");
printf("%s", num);
return 0;
}
PAT Advanced 1023 Have Fun with Numbers (20) [⼤整数运算]的更多相关文章
- PAT 甲级 1023 Have Fun with Numbers (20 分)(permutation是全排列题目没读懂)
1023 Have Fun with Numbers (20 分) Notice that the number 123456789 is a 9-digit number consisting ...
- PAT (Advanced Level) Practice 1120 Friend Numbers (20 分) (set)
Two integers are called "friend numbers" if they share the same sum of their digits, and t ...
- PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642
PAT (Advanced Level) Practice 1023 Have Fun with Numbers (20 分) 凌宸1642 题目描述: Notice that the number ...
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642
PAT (Advanced Level) Practice 1042 Shuffling Machine (20 分) 凌宸1642 题目描述: Shuffling is a procedure us ...
- PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642
PAT (Advanced Level) Practice 1041 Be Unique (20 分) 凌宸1642 题目描述: Being unique is so important to peo ...
- 1023 Have Fun with Numbers (20 分)
1023 Have Fun with Numbers (20 分) Notice that the number 123456789 is a 9-digit number consisting ...
- PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642
PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642 题目描述: A reversible prime in any n ...
- PAT 甲级 1023 Have Fun with Numbers(20)(思路分析)
1023 Have Fun with Numbers(20 分) Notice that the number 123456789 is a 9-digit number consisting exa ...
随机推荐
- 六十三、SAP中的逻辑运算符
一.SAP中逻辑运算符包括AND, NOT, OR 二.输出如下
- 【机器学习实战学习笔记(2-2)】决策树python3.6实现及简单应用
文章目录 1.ID3及C4.5算法基础 1.1 计算香农熵 1.2 按照给定特征划分数据集 1.3 选择最优特征 1.4 多数表决实现 2.基于ID3.C4.5生成算法创建决策树 3.使用决策树进行分 ...
- 4. Retrieving a mapper(检索映射器)
Retrieving a mapper(检索映射器) 4.1. The Mappers factory(映射工厂) 可以通过 org.mapstruct.factory.Mappers 类检索映射器实 ...
- 数论 CF27E Number With The Given Amount Of Divisors
求因子数一定的最小数(反素数) #include<iostream> #include<string> #include<cmath> #include<cs ...
- 吴裕雄--天生自然C++语言学习笔记:C++ 信号处理
信号是由操作系统传给进程的中断,会提早终止一个程序.在 UNIX.LINUX.Mac OS X 或 Windows 系统上,可以通过按 Ctrl+C 产生中断. 有些信号不能被程序捕获,但是下表所列信 ...
- python 中的os.path.split()函数用法
基本概念 os.path.split()通过一对链表的头和尾来划分路径名.链表的tail是是最后的路径名元素.head则是它前面的元素. 举个例子: path name = '/home/User ...
- 八、SAP中使用MOVE来赋值
一.使用MOVE和使用等号的效果是等同的,代码如下: 二.效果如下:
- 干货|浅谈iOS端短视频SDK技术实现
短视频SDK主要包含"视频录制"和"视频编辑"这两个核心功能. 视频录制包括:视频采集.美颜.滤镜.摄像头切换.视音频采集参数设置等功能: 视频编辑包括:视频导 ...
- SQL审核平台-Yearning安装部署实践
相关文档: https://guide.yearning.io/ yearning简介 http://python.yearning.io/install/ yearning安装 Yearning ...
- Mac下使用Hexo搭建个人博客
Hexo介绍 利用原作者的一句话:A fast,simple&powerful blog framework,powered by Node.js Hexo是基于Node.js的博客平台,He ...