zoukankan      html  css  js  c++  java
  • pat 1023. Have Fun with Numbers (20)

    1023. Have Fun with Numbers (20)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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
    解:存进位,存每个位数上的值,计算下就行了。

    代码:

    //Have Fun with Numbers
    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int main()
    {
        string s;
        cin>>s;
        int len=s.length();
        int *a=new int[len+1],*b=new int[len+1];
        int *temp1=new int[len+1],*temp2=new int[len+1];
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        memset(temp1,0,sizeof(temp1));
        memset(temp2,0,sizeof(temp2));
        for(int i=0;i<len;i++)
        {
            a[i]=s[i]-48;
            temp1[i]=a[i];
        }
        sort(temp1,temp1+len);
        int flag=1,c=0;
        for(int i=len-1;i>=0;i--)
        {
            a[i]=a[i]*2+b[i];
            if(i>0)
                b[i-1]=a[i]/10;
            if(a[i]>9)
            {
                if(i==0)
                    c=a[i]/10;
                a[i]=a[i]%10;
            }
        }
        for(int i=0;i<len;i++)
        {
            temp2[i]=a[i];
        }
        sort(temp2,temp2+len);
        for(int i=0;i<len;i++)
        {
            if(temp1[i]!=temp2[i])
            {
                flag=0;
                break;
            }
        }
        if(flag==1)
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
        if(c!=0) cout<<c;
        for(int i=0;i<len;i++)
        {
            cout<<a[i];
        }
        cout<<endl;
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    将软件做成服务,并开机自启
    ubuntu下sublime Text3配置C++编译环境
    StratifiedKFold与GridSearchCV版本前后使用方法
    o2o优惠券使用预测
    Meshgrid函数的基本用法(转载)
    Andrew Ng机器学习课程,第一周作业,python版本
    多元高斯分布(转载)
    Tensorflow图像处理
    TensorFlow最佳实践样例
    个人资源索引
  • 原文地址:https://www.cnblogs.com/Tobyuyu/p/4965286.html
Copyright © 2011-2022 走看看