首页 > 代码库 > Have Fun with Numbers (大数)
Have Fun with Numbers (大数)
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:
1234567899
Sample Output:
Yes
2469135798
1 #include <iostream> 2 3 #include <string> 4 5 #include <algorithm> 6 7 using namespace std; 8 9 10 11 int aa[21]; 12 13 int v1[10]; 14 15 int v2[10]; 16 17 18 19 int main() 20 21 { 22 23 24 25 string ss; 26 27 while(cin>>ss) 28 29 { 30 31 32 33 int i; 34 35 for(i=0;i<21;i++) 36 37 { 38 39 aa[i]=0; 40 41 } 42 43 for(i=0;i<10;i++) 44 45 { 46 47 v1[i]=0; 48 49 v1[i]=0; 50 51 } 52 53 int count=0; 54 55 for(i=ss.length()-1;i>=0;i--) 56 57 { 58 59 aa[count++]=ss[i]-‘0‘; 60 61 v1[ss[i]-‘0‘]=1; 62 63 } 64 65 66 67 for(i=0;i<count;i++) 68 69 { 70 71 aa[i]=aa[i]*2; 72 73 } 74 75 76 77 int tem,len; 78 79 for(i=0;i<count;i++) 80 81 { 82 83 if(aa[i]>9) 84 85 { 86 87 tem=aa[i]/10; 88 89 aa[i+1]=aa[i+1]+tem; 90 91 aa[i]=aa[i]%10; 92 93 } 94 95 } 96 97 98 99 if(aa[count]==0) len=count;100 101 else len=count+1;102 103 104 105 106 107 reverse(aa,aa+len);108 109 110 111 for(i=0;i<len;i++)112 113 {114 115 v2[aa[i]]=1;116 117 }118 119 bool ifis=true;120 121 for(i=0;i<10;i++)122 123 {124 125 if(v1[i]!=v2[i]) ifis=false;126 127 }128 129 130 131 if(ifis) cout<<"Yes"<<endl;132 133 else cout<<"No"<<endl;134 135 136 137 for(i=0;i<len;i++)138 139 {140 141 cout<<aa[i];142 143 }144 145 cout<<endl;146 147 148 149 }150 151 return 0;152 153 }
Have Fun with Numbers (大数)