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.
算法简单,但10^20超过long long 的数据范围,注意用string存数字就好。
代码如下:
#include<iostream>
using namespace std;
int digit[10]={0};
int ss[20]={0};
int main()
{
string s;
cin>>s;
int len=s.size();
int len1=len;
if(s[0]>='5')
len1++;
int d;
int m=0;
for(int i=len-1;i>=0;i--){
d=int(s[i]-'0');
digit[d]++;
d=d*2;
ss[m]+=d%10;
ss[m+1]=d/10;
m++;
}
for(int i=0;i<len1;i++)
digit[ss[i]]--;
bool flag=false;
for(int i=0;i<10;i++)
if(digit[i]!=0){
flag=true;
break;
}
if(!flag)
cout<<"Yes"<<endl;
else cout<<"No"<<endl;
for(int l=len1-1;l>=0;l--)
cout<<ss[l];
return 0;
}
样例结果如下: