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 ...
随机推荐
- Java对象序列化输入输出
在网上看到一篇有关于对象序列化的代码,自己仿着写了把 在Java中,entity通过implements Serializable,然后使用ObjectInputStream和ObjectOutput ...
- SQL中行转列(PIVOT)与列转行(UNPIVOT)
一.行转列 1.测试数据准备 CREATE TABLE [StudentScores] ( ), --学生姓名 ), --科目 [Score] FLOAT, --成绩 ) 执行结果: 2.行转列sql ...
- JDBC获取数据库连接慢
RHEL6.4 获取Oracle数据库链接时快时慢,RHEL7.3获取Oracle数据库链接时一直很慢能够达到五万毫秒. 解决方法: 修改$JAVA_HOME/jre/lib/security/jav ...
- spring源码 AutowireCapableBeanFactory接口
对于想要拥有自动装配能力,并且想把这种能力暴露给外部引用的BeanFactory类需要实现此接口.正常情况下,不要使用此接口应该更倾向于使用BeanFactory或者ListableBeanFacto ...
- linux系统pid的最大值研究
内核源码探查 通过对linux内核源码的追踪,可以看到对pid最大值的限定最终集中到include/linux/threads.h文件中的PID_MAX_DEFAULT上了,代码如下: /* * Th ...
- 学习spring第五天 mybatis+spring的整合(maven多模块数据查询使用了分页和连接池),以及aop
mybatis+spring的整合: 导入的依赖:1.数据库连接:mysql-connector-java 2.连接池:druid 3.servlet:javax.servlet-api 4.jstl ...
- js库链接
1.autoHeightTextarea自适应高度的textarea是一款jquery插件,支持链式调用,支持设置最小行数.最小高度.最大行数和最大高度,在输入文字的时候实现textarea的高度自适 ...
- springboot (2.0以上)连接mysql配置
pom <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java&l ...
- iOS 多线程 GCD part3:API
https://www.jianshu.com/p/072111f5889d 2017.03.05 22:54* 字数 1667 阅读 88评论 0喜欢 1 0. 预备知识 GCD对时间的描述有些新奇 ...
- UML-设计模式-本地服务容错-代理模式
在<本地服务容错-适配器+工厂模式>中,总是优先尝试本地服务.但是,有时候需要先尝试外部服务,然后才是本地服务.GoF的代理模式可以解决这个问题. 1.代理模式的一般结构 2.使用代理模式 ...