• PAT 1021 Have Fun With Numbers


    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
    

    Sample Output:

    1. Yes
    2. 2469135798

    总结:这道题目还是比较简单的,使用高精度乘法计算结果,用一个数组存储原始数字的出现次数,然后将这个数组和相乘的结果作比较,看是否是多出数字或者少了数字(不过代码看着有点多了)

    1. #include
    2. #include
    3. using namespace std;
    4. vector<int> mul(vector<int> &a,int b){
    5. int t=0;
    6. vector<int> c;
    7. for(int i=0;isize() || t;i++){
    8. if(isize()) t+=a[i]*b;
    9. c.push_back(t%10);
    10. t/=10;
    11. }
    12. while(c.size()>1 && c.back()==0) c.pop_back();
    13. return c;
    14. }
    15. int main(){
    16. string s,w;
    17. vector<int> a;
    18. int t[11]={0};
    19. cin >> s;
    20. for(int i=s.size()-1;i>=0;i--){
    21. a.push_back(s[i]-'0');
    22. t[s[i]-'0']++;
    23. }
    24. auto c=mul(a,2);
    25. bool st=true;
    26. for(int i=c.size()-1;i>=0;i--){
    27. t[c[i]]--;
    28. if(t[c[i]]<0){
    29. st=false;
    30. break;
    31. }
    32. }
    33. if(st) puts("Yes");
    34. else puts("No");
    35. for(int i=c.size()-1;i>=0;i--) printf("%d",c[i]);
    36. return 0;
    37. }

     依照惯例,还是看看大佬的代码:

    还是那么的简洁明了,通俗易懂!

    直接用原始数字的长度进行计算,这样加入有进位的话,最后flag(表示进位值)会等于1,如果没有进位的话,如果数字不一样flag1会等于1,最后就能判断 Yes 还是 No 了

    1. #include
    2. #include
    3. using namespace std;
    4. int book[10];
    5. int main() {
    6. char num[22];
    7. scanf("%s", num);
    8. int flag = 0, len = strlen(num);
    9. for(int i = len - 1; i >= 0; i--) {
    10. int temp = num[i] - '0';
    11. book[temp]++;
    12. temp = temp * 2 + flag;
    13. flag = 0;
    14. if(temp >= 10) {
    15. temp = temp - 10;
    16. flag = 1;
    17. }
    18. num[i] = (temp + '0');
    19. book[temp]--;
    20. }
    21. int flag1 = 0;
    22. for(int i = 0; i < 10; i++) {
    23. if(book[i] != 0)
    24. flag1 = 1;
    25. }
    26. printf("%s", (flag == 1 || flag1 == 1) ? "No\n" : "Yes\n");
    27. if(flag == 1) printf("1");
    28. printf("%s", num);
    29. return 0;
    30. }

    好好学习,天天向上!

    我要考研!

  • 相关阅读:
    docker-compose部署mysql
    Flink 简单 入门大纲
    网络攻防中黑客常用技术跨站脚本技术:html, js 的自解码机制,解码顺序,浏览器urlencode 的影响,测试样例
    多选题分析汇总
    手机\固定电话座机呼叫转移设置方法
    vue2的基础知识巩固
    我的2022面试总结(已拿BAT头条网易滴滴亚马逊offer)
    基于Docker部署Dubbo+Nacos服务
    3D打印喷嘴大小如何选择0.2-0.5mm喷嘴
    CentOS-7下安装及配置vsftpd详细步骤(可匿名访问)
  • 原文地址:https://blog.csdn.net/weixin_50679551/article/details/126818969