1023. 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 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:1234567899Sample Output:
Yes 2469135798
代码:
//Have Fun with Numbers #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; int main() { string s; cin>>s; int len=s.length(); int *a=new int[len+1],*b=new int[len+1]; int *temp1=new int[len+1],*temp2=new int[len+1]; memset(a,0,sizeof(a)); memset(b,0,sizeof(b)); memset(temp1,0,sizeof(temp1)); memset(temp2,0,sizeof(temp2)); for(int i=0;i<len;i++) { a[i]=s[i]-48; temp1[i]=a[i]; } sort(temp1,temp1+len); int flag=1,c=0; for(int i=len-1;i>=0;i--) { a[i]=a[i]*2+b[i]; if(i>0) b[i-1]=a[i]/10; if(a[i]>9) { if(i==0) c=a[i]/10; a[i]=a[i]%10; } } for(int i=0;i<len;i++) { temp2[i]=a[i]; } sort(temp2,temp2+len); for(int i=0;i<len;i++) { if(temp1[i]!=temp2[i]) { flag=0; break; } } if(flag==1) cout<<"Yes"<<endl; else cout<<"No"<<endl; if(c!=0) cout<<c; for(int i=0;i<len;i++) { cout<<a[i]; } cout<<endl; }
版权声明:本文为博主原创文章,未经博主允许不得转载。