码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【PAT甲级】1023 Have Fun with Numbers


    ✍个人博客:https://blog.csdn.net/Newin2020?spm=1011.2415.3001.5343
    📚专栏地址:PAT题解集合
    📝原题地址:题目详情 - 1023 Have Fun with Numbers (pintia.cn)
    🔑中文翻译:趣味数字
    📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!
    ❤️如果有收获的话,欢迎点赞👍收藏📁,您的支持就是我创作的最大动力💪

    1023 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 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
    
    • 1

    Sample Output:

    Yes
    2469135798
    
    • 1
    • 2
    题意

    这道题是让我们计算给定的数字加倍过后的结果是否满足题目要求,比如 123456789 加倍过后得到 246913578 ,发现加倍后的数字都完全由加倍前的数字构成,即加倍前和加倍后都用到了相同的数字构成。

    现在让我们输出加倍后的结果,如果满足要求就输出 Yes ,否则输出 No ,并且还要输出加倍后的结果。

    思路

    这道题由于数字的长度最多能取到 20 ,所以要用到高精度的算法。因为题目只要求算给定数字的平方,故我们可以直接计算该数自加的结果即 a ∗ 2 = a + a a * 2 = a + a a∗2=a+a ,这样就可以将高精度乘法问题转换为高精度加法问题,然后再套用高精度加法的模板即可求出本题的答案。

    代码
    #include
    using namespace std;
    
    int main()
    {
        string str;
        cin >> str;
    
        vector<int> a;
        for (int i = str.size() - 1; i >= 0; i--)    a.push_back(str[i] - '0');
    
        //高精度加法运算模板,a*2=a+a
        vector<int> b;
        int t = 0;
        for (int i = 0; i < a.size(); i++)
        {
            t = a[i] + a[i] + t;
            b.push_back(t % 10);
            t = t / 10;
        }
        if (t) b.push_back(t);
    
        //判断是否满足条件
        vector<int> c = b;
        sort(a.begin(), a.end());
        sort(c.begin(), c.end());
        if (a == c)    puts("Yes");
        else    puts("No");
    
        //输出加倍后的数字
        for (int i = b.size() - 1; i >= 0; i--)  cout << b[i];
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
  • 相关阅读:
    MySQL中的大表优化方案
    ubuntu| sudo apt-get update 更新失败, 没有 Release 文件 无法安全地用该源进行更新,所以默认禁用该源
    常用sql语句
    Android 12(S) 图像显示系统 - GraphicBuffer同步机制 - Fence
    Unity & PS Linear Workflow - Unity 和 PS 的线性工作流实践 - 简单配置示例(后续补上渲染差异图)
    C++编程基础
    OpenGauss的TPCE测试
    Spring Boot Logback启动流程
    OpenText™ Exceed™ TurboX (ETX) 助力医疗保健信息系统开发商将远程访问的稳定性提高了 95%
    蓝桥杯入门(三)卡片
  • 原文地址:https://blog.csdn.net/Newin2020/article/details/126731722
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号