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 ...
随机推荐
- React Native 教程:001 - 如何运行官方控件示例 App
原文发表于我的技术博客 本文主要讲解了如何运行 React Native 官方控件示例 App,包含了一些 React Native 的基础知识以及相关环境的配置. 原文发表于我的技术博客 React ...
- zabbix监控主机cpu达到80%后报警
在zabbix监控中,默认cpu监控模板中的触发器,当负载在一定时间内(比如最近5分钟)超过5以上为报警阀值.但是在实际场景中,由于服务器配置不一样,这个默认的cpu触发器用起来意义就不大了,这时候就 ...
- python之requests
发送请求 导入 Requests 模块: >>> import requests >>> r = requests.get('https://xxxxxxx.jso ...
- Pairproject 移山之道 阅读随笔和一些问题
首先不得不承认这本书的写作方式很独特,不像其他的计算机类的图书那样枯燥,让人读起来感觉很有意思,他也颠覆了我对计算机类图书的看法,这种写作方式值得我们学习. 先谈谈收获吧.读了两年大学,这是第一次写类 ...
- Appium学习笔记1_获取到APK安装包的Package以及Activity属性值
我们设置DesiredCapabilities属性值得时候需要设置"appPackage"和"appActivity",如何获取到这两个值呢? 这两个值不是随便 ...
- 二叉搜索树(BST)
(第一段日常扯蛋,大家不要看)这几天就要回家了,osgearth暂时也不想弄了,毕竟不是几天就能弄出来的,所以打算过完年回来再弄.这几天闲着也是闲着,就掏出了之前买的算法导论看了看,把二叉搜索树实现了 ...
- NAT模式下VMware中CentOS7无法连接外网的解决方法
故障现象 ----------------------------------------------------------------------------------------------- ...
- loadrunner报错
Action.c(1516): Error -27727: Step download timeout (120 seconds) has expired when downloading resou ...
- delphi 通过事务插入数据
orsn1.StartTransaction; try qry1.Sql.Clear; qry1.Sql.Text:=' select * from log '; qry1.Open; qry1.In ...
- 关于JavaScript中this的软绑定
首先,什么是软绑定? 所谓软绑定,是和硬绑定相对应的一个词,在详细解释软绑定之前,我们先来看看硬绑定.在JavaScript中,this的绑定是动态的,在函数被调用的时候绑定,它指向什么完全取决于函数 ...