• PAT 甲级 1023 Have Fun with Numbers (20 分)(permutation是全排列题目没读懂)


    1023 Have Fun with Numbers (20 分)

     

    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

    解题思路:
      给定一个数,乘以2之后,各个数位的出现次数,是否与乘之前是否相同,相同输出Yes,否认输出No,然后下一行打印出乘2之后的各个数位。
      这个题目是属于超大数运算的类型,题目给出的最高数位是20位,那么即便是用最高的unsigned long long 也会面临溢出的情况,所以输入和输出,只能用string,诸位乘2,然后再记录每一位出现的次数,相比较就行。

         大数乘法!全排列的意思是:各个数字出现的次数分别相同!

    AC代码:

    #include<bits/stdc++.h>
    using namespace std;
    char a[21];
    char b[21];
    int ori[11];
    int ne[11];
    int main(){
        memset(ori,0,sizeof(ori));
        memset(ne,0,sizeof(ne));
        cin>>a;
        //先反着放
        int l=strlen(a);
        for(int i=l-1;i>=0;i--){
            b[l-i]=a[i];
            ori[a[i]-'0']++;
        } 
        //大数乘法 
        int x=0;
        for(int i=1;i<=l;i++){
            int y=b[i]-'0';
            y*=2;y+=x;
            b[i]=y%10+'0';
            x=y/10;    
            ne[b[i]-'0']++;            
        }
        int l2=l;
        if(x!=0){
            l2++;
            b[l2]=x+'0';
            ne[b[l2]-'0']++;    
        }
        //判断 
        int f=1;
        for(int i=0;i<=9;i++){
            if(ne[i]!=ori[i]){
                f=0;
                break;
            }
        }
        if(f){
            cout<<"Yes"<<endl;
        }else{
            cout<<"No"<<endl;
        }
        for(int i=l2;i>=1;i--){
            cout<<b[i];
        }
        return 0;
    } 
     
  • 相关阅读:
    Angular实现数据绑定,它实现原理是什么?
    用angular实时获取本地localStorage数据,实现一个模拟后台数据登入的效果
    巨坑
    Hack 语言学习/参考---1.3 Summary
    Hack 语言学习/参考---1.2 Hack Background
    Hack 语言学习/参考---1.1 What is Hack?
    Hack 语言学习/参考---1.Hack 语言
    HHVM Installation and Configuration(HHVM 安装及配置)
    What is HHVM?
    Facebook HHVM 和 Hack 手册 --- 2. HHVM能做什么
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/11356050.html
Copyright © 2020-2023  润新知