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 ...
随机推荐
- 使用TestNG-xslt美化测试报告
用TestNG测试后,自动会生成html的测试报告,不过相信大家都有感觉,自动生成的测试报告太难看了,所以我们又用了ReportNG来美化它.在 这里给大家再介绍一下比reportNG还要稍稍美观一点 ...
- c++程序—输入
#include<iostream> using namespace std; #include<string> int main() { string str = " ...
- 找《飘》 中最常用的N个单词。
找<飘> 中最常用的N个单词. 1,题目:输出单个文件(<飘> 英文版)中的前 N 个最常出现的英语单词,并将结果输入到文本文件中. 2,设计思路: 1),按行依次读取文件并按 ...
- hash表系列(转)
http://www.cnblogs.com/mumuxinfei/p/4441826.html 前言: 我以前在百度的mentor, 在面试时特喜欢考察哈希表. 那时的我满是疑惑和不解, 觉得这东西 ...
- 一、VIP课程:互联网工程专题 01-Git基本概念与核心命令掌握
第一课:Git基本概念与核心命令掌握.docx 课程概要: GIT 体系概述 GIT 核心命令使用 GIT 底层原理 一.GIT体系概述 1.使用方式区别 从本地把文件推送远程服务,SVN只需要com ...
- swiper用axios异步请求后 循环失效
解决方案 使用ajax动态获取数据 当数据还没有收到的时候,swiper组件收到的是跟组件data传过来的空数组,会造成渲染问题 这个时候可以给swiper组件 设置一个 v-if='list.l ...
- offsetof宏与container_of宏
offsetof宏与container_of宏1.由结构体指针进而访问各元素的原理(1)通过结构体整体变量来访问其中各个元素,本质上是通过指针方式来访问的,形式上是通过.的方式来访问的(这个时候其实是 ...
- outlook 2013邮件在服务器保留副本
用outlook2013来收邮件确实是比较方便,但是它收邮件默认设置是:当outlook2013将在线邮箱的邮件下载至本机计算机之后,它就会删除在线邮箱中的邮件.不知道是不是以前邮箱容量比较小,所以要 ...
- git push的时候.gitignore不起作用的解决方法
问题的原因 这是因为在你添加.gitignore之前已经进行过push操作,有些文件已经纳入版本管理了. 解决方法 我们就应该先把本地缓存删除,然后再进行git的push,这样就不会出现忽略的文件了. ...
- C++11多线程访问时候的数据保护实例
#include<iostream> #include<thread> #include<string> #include<vector> #inclu ...