7-8 Have Fun with Numbers (20 分)

7-8 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
//数字出现的次数输入前与变换后是否相等,检查范围是【0,9】;
//数字的总位数输入前与输入后是否相等;
//易错样例:59,第一位数2之后大于等于10;应输出No 118;
//输入53输出No 106;
//输入999输出No 1998;
#include<stdio.h>
#include<string.h>
int main()
{
char ch[1000];
int i,j,a[1000]={0},temp,k=0,n1[1000]={0},n2[1000]={0},flag=0,temp1,len=0;
gets(ch);
len=strlen(ch);//原来数字的位数;
for(i=0;ch[i]!=’\0’;i++)
{
temp=ch[i]-‘0’;
temp1=temp
2;
n1[temp]++;//统计原始数字出现的次数;
if(temp1<10) a[k++]=temp1%10;
else if(temp1>=10)
{
if(i= =0)//输入59或53;
{
a[k]=temp1/10;//把十位数赋值给a[0];
k++;
a[k]=temp1%10;//把个位数赋值给a[1];
k++;
}
else if(i>=1)
{
a[k-1]=a[k-1]+temp1/10;//向高位进位;
a[k++]=temp1%10;//保留了进位之后的数字
}
}
}
for(i=0;i<k;i++)//k是变换之后数字的位数;
{
n2[a[i]]++;//变换之后的数字出现的次数;
}
for(i=0;i<=9;i++)//若输入以5结尾的数1234567895;则二倍之后末尾是零;
{
if(n1[i]!=n2[i])//比较一下原来的数字和变换之后的数字出现得次数是否相同;
{
flag=1;
break;
}
}
if(flag= =0&&k==len)
{
printf(“Yes\n”);
for(i=0;i<k;i++)
{
printf("%d",a[i]);
}
}
else
{
printf(“No\n”);
for(i=0;i<k;i++)
{
printf("%d",a[i]);
}
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43788669/article/details/87859148