关闭

1023. Have Fun with Numbers (20)

138人阅读 评论(0) 收藏 举报
分类:

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 <string.h>
void DoubleNum(char s1[],int n,char s2[]);
int Check(char s1[],char s2[]);
int main(){
    char s1[25],s2[25];
    scanf("%s",s1);
    DoubleNum(s1,strlen(s1),s2);
    if(Check(s1,s2))
        printf("Yes\n%s",s2);
    else printf("No\n%s",s2);
}
void DoubleNum(char s1[],int n,char s2[]){
    int i,add=0;
    for(i=n-1;i>=0;i--){
        s2[i]=((s1[i]-'0')*2+add)%10+'0';
        add=((s1[i]-'0')*2+add)/10;
    }
    if(add){
        for(i=n;i>0;i--)
            s2[i]=s2[i-1];
        s2[0]=add+'0';
        s2[n+1]='\0';
    }
    else s2[n]='\0';
}
int Check(char s1[],char s2[]){
    int c[10]={0},i;
    if(strlen(s1)!=strlen(s2))r
        eturn 0;
    for(i=0;i<strlen(s1);i++)
        c[s1[i]-'0']++;
    for(i=0;i<strlen(s2);i++)
        c[s2[i]-'0']--;
    for(i=0;i<10;i++)
        if(c[i]!=0)return 0;
    return 1;
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:42126次
    • 积分:2777
    • 等级:
    • 排名:第12960名
    • 原创:248篇
    • 转载:6篇
    • 译文:0篇
    • 评论:8条
    最新评论