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:
Yes2469135798
#include <iostream> #include <cstring> using namespace std; void counting(char *str,int a[]){ while(*str !='\0'){ a[*str -'0']++; str++; } } char * doublex(char a[],int n){ char *p=new char[n+1]; int carry=0; int tmp=0; int i=n; while(i>=1){ tmp =(a[i-1]-'0')*2; p[i]=carry+tmp%10+'0'; carry=tmp/10; i--; } if(carry){ p[i]='1'; return p; }else return p+1; } bool compare(int a[],int b[]){ int i=0; for(;i<10;i++){ if(a[i] !=b[i]) return false; } return true; } int main() { char str[30]; int a[10]={0,0,0,0,0,0,0,0,0,0}; int b[10]={0,0,0,0,0,0,0,0,0,0}; cin >>str; int length = strlen(str); char *p=doublex(str,length); counting(str,a); counting(p,b); bool flag=compare(a,b); if(flag) cout <<"Yes" <<endl; else cout <<"No" <<endl; cout <<p <<endl; return 0; }