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 ...
随机推荐
- Helper
//检测端口是否使用 public static bool VerifyListenerPort(int port) { bool inUse = false; System.Net.NetworkI ...
- Notepad++列编辑
NotePad++列编辑 工具:Notepad++使用说明:在我们的日常工作中,经常会碰到要修改多行记录,一行行去处理会非常浪费人力,这时候列编辑就是一个很好的解决方法,列编辑在进行数据批量操作时是一 ...
- Mysql读写分离方案-Amoeba环境部署记录
Mysql的读写分离可以使用MySQL Proxy,也可以使用Amoeba.Amoeba(变形虫)项目是一个类似MySQL Proxy的分布式数据库中间代理层软件,是由陈思儒开发的一个开源的java项 ...
- linux alias 别名设置【转载】
功能说明:设置指令的别名. 语 法:alias[别名]=[指令名称] 形如: alias cp=“cp -i” : 补充说明:用户可利用alias,自定指令的别名.若仅输入alias,则可列出目前所有 ...
- Individual P1: Summary
经过5个小时成功把simple mode写差不多了..orz 也是蛮拼的. 开始毫无头绪,本能地开始从度娘搜索‘c# 单词统计’= =看了两段代码也算是见过c#的人了.差不多花了我1小时的时间. 然后 ...
- M1/M2阶段总结
之前提问的博客 问题解答 问题 1 关于代码复审,复审者是否应该参与编码?如果复审者也参与编码的话,那么难免任务量较多,但如果不参与编码的话,工作分配的似乎不太均衡. 我们的团队项目在M1和M2阶段没 ...
- Scrum Meeting NO.9
Scrum Meeting No.9 1.会议内容 2.任务清单 徐越 序号 近期的任务 进行中 已完成 1 代码重构:前端通讯模块改为HttpClient+Json √ 2 "我" ...
- 第九次Scrum meeting
第九次Scrum meeting 任务及完成度: 成员 12.31 1.1 陈谋 任务1040:完成stackoverflow的数据处理后的json处理(99%) 任务1114-1:完成对网页数据的 ...
- SRS用例
团队项目:超市管理系统 作者:王琨 个人博客地址:http://www.cnblogs.com/wangkun123 一. 用例视图概述 一般的超市商品管理系统,主要由五大模块组成,即商品信 ...
- HDOJ2017_字符串统计
这是一道水题 HDOJ2017_字符串统计 #include<iostream> #include<string> #include<stdio.h> #inclu ...