1023. Have Fun with Numbers (20)

来源:互联网 发布:通信网络与信息技术 编辑:程序博客网 时间:2024/05/21 06:33

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

题意:一个大数乘二之后和原数的数字组成是否相同(注意判断最高位如果乘二之后会进位的情况)

#include<stdio.h>#include<stdlib.h>#include<string.h>int main(){    int s[30];    int cnt1[30],cnt2[30];    char dit[30];    int i,j,k,flag,t,pos;    while(~scanf("%s",dit))    {        int n = strlen(dit);        memset(cnt1,0,sizeof(cnt1));        memset(cnt2,0,sizeof(cnt2));        j = 0;        for(i = n-1; i>=0; i--)        {            s[j] = dit[i]- '0';            cnt1[s[j++]]++;        }        k = 0;///k表示进位        for(i=0; i<n; i++)        {            t = (s[i]*2+k)/10;            s[i] = (s[i]*2+k)%10;            cnt2[s[i]]++;            k = t;        }        flag = 0;        for(i=0; i<=9; i++)        {            if(cnt1[i]!=cnt2[i])            {                flag = 1;                break;            }        }        if(flag) printf("No\n");        else printf("Yes\n");        if(k!=0)///如果最高位需要进位        {            pos = n;            s[n] = k;        }        else             pos = n-1;        for(i=pos; i>=0; i--)        {            printf("%d",s[i]);        }        printf("\n");    }    return 0;}
原创粉丝点击