首页 > 其他分享 >PAT Advanced 1023 Have Fun with Numbers(20)

PAT Advanced 1023 Have Fun with Numbers(20)

时间:2022-08-21 22:45:35浏览次数:83  
标签:digits 20 1023 int number Numbers 哈希 ha

题目描述:

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 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和2n使用相同次数的0 ~ 9,则输出Yes;否则输出No。并输出2n

#include<iostream>
#include<cstring>
#include<string>
using namespace std;

int ha[10];
bool check()
{
    for(int i = 0 ; i < 10 ; i ++)
        if(ha[i])
            return false;
    return true;
}

int main()
{
    string a, b, res;
    cin >> a;
    // 将a的各个位的数量存储在哈希表中 且 利用高精度加法原理存储两倍至b中
    int t = 0;
    for(int i = a.size() - 1 ; i >= 0 ; i --)
    {
        ha[a[i] - '0'] ++;
        t += (a[i] - '0') * 2;
        b += t % 10 + '0';
        t /= 10;
    }
    if(t)   b += '1';
    // 哈希表减去b中的每个位  并反转存储至res(高精度加法存储为逆序)
    for(int i = b.size() - 1 ; i >= 0 ; i --)
    {
        ha[b[i] - '0'] --;
        res += b[i];
    }
    // 如果最终哈希表中所有位都为0  则两个数串a,b匹配
    if(check())    cout << "Yes" << endl;
    else    cout << "No" << endl;
    cout << res;
    
    return 0;
}

标签:digits,20,1023,int,number,Numbers,哈希,ha
From: https://www.cnblogs.com/yztozju/p/16611247.html

相关文章

  • 随笔分类 - Microsoft Dynamices CRM(2013, 2011)
    随笔分类-MicrosoftDynamicesCRM(2013,2011)MicrosoftDynamicsCRM数据库连接存储位置在哪里是在注册表里摘要:MicrosoftDynamicsCRM数据库连接存储......
  • 2022河南萌新联赛第(七)场
    C机智的我一开始有选中的概率是\(\frac{1}{n}\),打开k个后的概率是\(\frac{n-1}{n}\times\frac{1}{n-k-1}\)所以\(k=0\)没差别,\(k>0\)更换一定更好#include<bits/std......
  • Linq-20220817更新
    一、常用函数Where:每一项数据都会经过predicate(传入的委托lambda表达式)的测试,如果对元素执行predicate后返回值为True,则这个元素会添加到结果数组中Count:每一项数据都......
  • 2022.8.21 线程池
    11、线程池(重点)线程池Executors:3大方法、7大参数、4种拒绝策略池化技术程序的运行,本质:占用系统的资源!优化资源的使用!==>引进了一种技术池化池线程池、连接池、内......
  • 2022-08-18 MySQL常用函数
    MySQL常用函数聚合函数count:计数。count(*)≈count(1)>count(主键)count(*):MySQL对count(*)底层优化,count(0)。count(1)count(主键)count(字段)min:最小值max:最......
  • 2022.8.21 四大函数式接口与Stream流式计算
    12、四大函数式接口(重点)   函数接口:只有一个方法的接口    @FunctionalInterface publicinterfaceRunnable{     publicabstractvoidrun(......
  • Altium Designer 20软件安装包下载及安装教程
    AltiumDesigner20软件安装包下载及安装教程AltiumDesigner20软件简介:AltiumDesigner20是一款由Altium开发团队全新推出的简单易用,与时俱进,功能强大的PCB设计软件,可......
  • 2022.8.21 Forkjoin与异步回调
    14、Forkjoin(分支合并)什么是ForkJoinForkJoin在JDK1.7, 并行执行任务!提高效率。在大数据量中!大数据:MapReduce(把大任务拆分为小任务)Forkjoin特点:工作窃取,这里......
  • [ZJOI2019]语言
    先讲一个智障3log做法,听说考场上不止一个人写还都过了。树剖,转化为\((u,v)\)的\(dfs\)序若都在一个区间内则它们可以开展贸易活动。相当于求矩形总面积,可以扫描线。......
  • 2022.8.21 读写锁与阻塞队列
    9、读写锁   自定义的缓存,没有加锁,就会出现一个没有写入完成,另一个突然插进来的情况 packagecom.xing.rw; ​ importjava.util.HashMap; importjava.util.......