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
提交代码
编译器
AWK (awk 4.0.1)
C (gcc 4.7.2)
C# (mcs 2.10.8.1)
C++ (g++ 4.7.2)
Go (gccgo 4.7.2)
Go (go 1.3)
Haskell (ghc 7.4.1)
Java (javac 1.6.0)
Java (gcj 4.7)
Javascript (node 0.10.33)
Lisp (clisp 2.49)
Lua (lua 5.2.1)
OCaml (ocamlc 3.12.1)
Pascal (fpc 2.6.0)
Perl (perl 5.14.2)
PHP (php 5.4.34)
Plaintext (cat 1.0)
Python (python3 3.2.3)
Python (python2 2.7.3)
Ruby (ruby 1.9.3)
Scheme (racket 5.2.1)
Shell (bash 4.2.37)
Vala (valac 0.16.1)
VisualBasic (vbnc 0.0.0.5943)
使用高级编辑器
代码
1
思路:利用大数相乘的方法
1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 int data[10]={ 6 0 7 }; 8 struct Bignum 9 { 10 int d[1000]; 11 int len; 12 Bignum() //构造函数 13 { 14 memset(d,0,sizeof(d)); 15 len=0; 16 } 17 }; 18 Bignum A; 19 void Mul() 20 { 21 int carry=0; 22 for(int i=0;i<A.len;i++) 23 { 24 A.d[i]=A.d[i]*2+carry; 25 if(A.d[i]>=10) 26 { 27 carry=A.d[i]/10; 28 A.d[i]%=10; 29 } 30 else 31 carry=0; 32 33 } 34 if(carry!=0) //此处有错误 35 { 36 A.len++; 37 A.d[A.len-1]=carry%10; 38 carry/=10; 39 } 40 } 41 int main(int argc, char *argv[]) 42 { 43 44 char ori[100]; 45 scanf("%s",ori); 46 int length=strlen(ori); 47 for(int i=length-1;i>=0;i--) 48 { 49 A.d[A.len++]=ori[i]-‘0‘; 50 data[ori[i]-‘0‘]++; 51 } 52 Mul(); 53 bool flag=true; 54 for(int i=A.len-1;i>=0;i--) 55 { 56 data[A.d[i]]--; 57 } 58 for(int i=0;i<10;i++) 59 { 60 if(data[i]!=0) 61 { 62 flag=false; 63 break; 64 } 65 } 66 if(flag) 67 printf("Yes\n"); 68 else 69 printf("No\n"); 70 for(int i=A.len-1;i>=0;i--) 71 { 72 printf("%d",A.d[i]); 73 } 74 putchar(‘\n‘); 75 return 0; 76 }