A1023. Have Fun with Numbers
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
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<string.h>
using namespace std;
char str[];
typedef struct info{
int num[];
int len;
info(){
for(int i = ; i < ; i++){
num[i] = ;
}
len = ;
}
}bign; bign a, c; bign add(bign a, bign b){
int carry = ;
bign c;
for(int i = ; i < a.len || i < b.len; i++){
int temp = a.num[i] + b.num[i] + carry;
c.num[i] = temp % ;
carry = temp / ;
c.len++;
}
if(carry != )
c.num[c.len++] = carry;
return c;
}
int main(){
int hashTB[] = {,};
scanf("%s", str);
for(int i = strlen(str) - ; i >= ; i--){
a.num[a.len] = str[i] - '';
hashTB[a.num[a.len]]++;
a.len++;
}
c = add(a, a);
for(int i = ; i < c.len; i++){
hashTB[c.num[i]]--;
}
int tag = ;
for(int i = ; i < ; i++){
if(hashTB[i] != ){
tag = ;
break;
}
}
if(tag == )
printf("Yes\n");
else printf("No\n");
for(int i = c.len - ; i >= ; i--){
printf("%d", c.num[i]);
}
cin >> str;
return ;
}
总结:
1、大整数的记录结构:
typedef struct info{
int num[];
int len;
info(){
for(int i = ; i < ; i++){ //全初始化为0,这样在做加法时可以直接循环到最长的数,而不是仅仅循环到最短的数就结束。
num[i] = ;
}
len = ;
}
}bign;
2、大整数的加法:
bign add(bign a, bign b){
int carry = ;
bign c;
for(int i = ; i < a.len || i < b.len; i++){ //以长的数位界
int temp = a.num[i] + b.num[i] + carry;
c.num[i] = temp % ;
carry = temp / ;
c.len++;
}
if(carry != )
c.num[c.len++] = carry;
return c;
}
A1023. Have Fun with Numbers的更多相关文章
- PAT甲级——A1023 Have Fun with Numbers
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wit ...
- [PAT] A1023 Have Fun with Numbers
[题目大意] 给一个不超过20位的数字,如果将它乘以2得到的数仅仅是原来的数字重新排列得到的,那就输出Yes,下一行输出加倍后的数.如果不是,输出No,下一行输出加倍后的数. [思路] 20位过于庞大 ...
- PAT_A1023#Have Fun with Numbers
Source: PAT A1023 Have Fun with Numbers (20 分) Description: Notice that the number 123456789 is a 9- ...
- PAT 大数运算
PAT中关于大数的有B1017,A1023,A1024 (A-Advance,B-Basic) B1017 1017. A除以B (20) 本题要求计算A/B,其中A是不超过1000位的正整数,B是1 ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- Java 位运算2-LeetCode 201 Bitwise AND of Numbers Range
在Java位运算总结-leetcode题目博文中总结了Java提供的按位运算操作符,今天又碰到LeetCode中一道按位操作的题目 Given a range [m, n] where 0 <= ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- [LeetCode] Add Two Numbers II 两个数字相加之二
You are given two linked lists representing two non-negative numbers. The most significant digit com ...
- [LeetCode] Maximum XOR of Two Numbers in an Array 数组中异或值最大的两个数字
Given a non-empty array of numbers, a0, a1, a2, … , an-1, where 0 ≤ ai < 231. Find the maximum re ...
随机推荐
- ABP module-zero +AdminLTE+Bootstrap Table+jQuery权限管理系统第十四节--后台工作者HangFire与ABP框架Abp.Hangfire及扩展
返回总目录:ABP+AdminLTE+Bootstrap Table权限管理系统一期 HangFire与Quartz.NET相比主要是HangFire的内置提供集成化的控制台,方便后台查看及监控,对于 ...
- VS2010、VS2012、VS2013、VS2015、VS2017各版本产品激活秘钥
Visual Studio 2017(VS2017) 企业版 Enterprise 注册码:NJVYC-BMHX2-G77MM-4XJMR-6Q8QF Visual Studio 2017(VS201 ...
- 第三次作业(1) Visual Studio程序安装过程和练习过程
Visual Studio程序安装过程和练习过程 第一步 首先要在网上找一个VS2013的安装包,之后我安装在D盘上,C盘上也需要有5.2G空间,勾选相应的选项,才能继续安装. 安装的过程很漫长,接近 ...
- 《Linux内核分析》第八周学习总结
<Linux内核分析>第八周学习总结 ——进程的切换和系统的一般执行过程 姓名:王玮怡 学号:20135116 ...
- Linux内核分析 读书笔记 (第七章)
第七章 链接 1.链接是将各种代码和数据部分收集起来并组合成为一个单一文件的过程,这个文件可被加载(或被拷贝)到存储器并执行. 2.链接可以执行于编译时,也就是在源代码被翻译成机器代码时:也可以执行于 ...
- Enterprise Integration Patterns
https://camel.apache.org/enterprise-integration-patterns.html 企业集成模式,各种模式算法,挺棒的. https://camel.apach ...
- VSCODE安装以及使用Python运行调试代码的简单记录
1. VScode安装 官网下载VSCODE https://code.visualstudio.com/ 下载呢windows的x64安装包,安装stable的版本 当前日期 2018.01.15 ...
- 在使用ADOQuery删除数据时的处理 [问题点数:100分,结帖人isdxsc]
在使用ADOQuery删除数据时的,希望在他的事件BeforeDelete进行一些判断,符合要求的进行删除,不符合要求的终止这个删除行为,请问应该用什么语句呢?还有个比较奇怪的现象也一起请教:DBGr ...
- 深入理解CSS绝对定位absolute
前面的话 前面已经介绍了定位的偏移和层叠,例子中大量的应用了绝对定位.因为相较于相对定位和固定定位,绝对定位在实际中应用频率更高.应用场景更广泛.本文将介绍使用绝对定位时的具体细节 定义 当元素绝对定 ...
- codeforces622B
The Time CodeForces - 622B 给你当前的时间(24小时制):HH:MM.输出 x 分钟后的时间是多少?(24小时制) 不明白可以看看例子哦- Input 第一行给出了当前时间, ...