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
#include<bits/stdc++.h>
using namespace std;
int main()
{
int i=0,k,j,jj,f,m,c,aa,l;
int a[20],b[21];
char n[21];
memset(b,0,sizeof(b));
scanf("%s",n);
for(i=0;i<strlen(n);i++){ //用数组a存输入的数字
a[i]=(n[i]-'0');
}
for(j=i-1,jj=i;j>=0,jj>0;j--,jj--) //用数组b存加倍后的结果
{
b[jj]=b[jj]+a[j]*2;
if(b[jj]>9)
{
aa=b[jj];
b[jj]=aa%10;
b[jj-1]=aa/10;
}
}
if(b[0]!=0) //判断b的第一个数是否为0,若非零,则加倍后位数加一,一定不满足
{
cout<<"No\n";
for(l=0;l<i+1;l++)
cout<<b[l];
return 0;
}
for(k=0;k<i;k++) //位数与之前相同的情况
{
f=0;
for(m=1;m<i+1;m++)
{
if(a[k]==b[m])
{
f=1;
break;
}
}
if(f==0) //若存在一个数对不上,则不满足结果,
{
cout<<"No\n";
for(l=1;l<i+1;l++)
cout<<b[l];
return 0;
}
}
cout<<"Yes\n"; //满足结果情况
for(l=1;l<i+1;l++)
cout<<b[l];
return 0;
}