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:
Yes

2469135798

可以说是第一次做这种全英文的题目,在题意的读取理解上还是有问题的。(以后还是要多记单词啊微笑)。

题目的大体意思是有一个位数不超过20位的数字,你先把他加倍一下,即*2;得到一个别的数。你对新得到的数字进行检查看看是否存在的数字以及该数字出现的次数与为加倍的相比相不相同,如果相同输出Yes 不同输出No,并且无论时yes或no都要在最后输出你加倍后得到的数。

思路:在这里位数最大为20,超出了long long int 的范围,所以我们要采用字符串输入。但要注意将字符串转化为int型的方法。因为字符串都是ASCII码的值,所以要这样做:

  for(i=0; l[i]!='\0'; i++)
    {
        if(l[i]!='\0')
        {
            a[i]=l[i]-'0';减去0的ascii码就是你想要得到的数字;
            b[i]=a[i]*2;
           
        }

    }

另外还需特别注意的就是进位问题,具体步骤看如下代码吧:(要注意输出yes的前提是位数要相同,如果b[0]大于9了说明加倍后的数比原来的多了一位就可以直接输出no了)AC代码如下:

测试点	提示	结果	耗时	内存
0	跟sample完全一样,超过long,但unsigned long可以;有2个重复数字	答案正确	3 ms	512KB
1	long可以,答案是No,位数不匹配	答案正确	2 ms	384KB
2	10个数字全出现,unsigned long可以,答案Yes	答案正确	2 ms	512KB
3	超过unsigned long,输入中有数字不在结果中,但结果数字都在输入里	答案正确	2 ms	508KB
4	最长串,10个数字全出现	答案正确	2 ms	512KB
5	最长串全是9	答案正确	3 ms	380KB
代码
#include<bits/stdc++.h>
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
    char l[21];//因为最大数值超出long long int的范围,所以采用字符串输入;
    int a[21],b[22];//用来记录转换的值;
    int n=0,i,j;//标记数的长度;
    scanf("%s",l);
    for(i=0; l[i]!='\0'; i++)
    {
        if(l[i]!='\0')
        {
            a[i]=l[i]-'0';
            b[i]=a[i]*2;
            n++; //记录输入数据的位数;
        }

    }
    for(i=n-1; i>0; i--) //进位;
    {
        for(j=i; j>0; j--)
        {
            if(b[j]>9)
            {
                b[j]-=10;
                b[j-1]+=1;
            }
        }
    }
    //判别;
    int a1[10]= {0,0,0,0,0,0,0,0,0,0};
    int a2[10]= {0,0,0,0,0,0,0,0,0,0};
    if(b[0]>9)
        printf("No\n");
    else
    {
        for(i=1; i<10; i++)
        {
            for(j=0; j<10; j++)
            {
                 if(a[j]==i)
                    a1[i]++;
                 if(b[j]==i)
                    a2[i]++;
            }
        }
        for(i=1;i<10;i++)
        {
            if(a1[i]==a2[i]);
            else
                break;
        }
        if(i==10)
            printf("Yes\n");
        else
            printf("No\n");
    }
    for(i=0;i<n;i++)
        printf("%d",b[i]);

}
up up up!

猜你喜欢

转载自blog.csdn.net/z_xindong/article/details/80913228